2010年9月23日木曜日

LosFormatter

Validation of viewstate MAC failed error - Stack Overflow

なんか面白そう~、な感じだったので試してみたけど、思った結果にはなりませんでした。

  public class HomeController : Controller
  {
    public ActionResult Index()
    {
      var person = new Person {Name = "ルフィー", Age = 18};
      
      // machinekey
      var macKeyModifier = "";
      var machineKey = WebConfigurationManager.GetSection("system.web/machineKey") as MachineKeySection;
      if(machineKey!=null)
      {
// DecryptionKeyでも同じでした... macKeyModifier = machineKey.ValidationKey; } //plain var los = new LosFormatter(); using (var writer = new StringWriter()) { los.Serialize(writer,person); ViewData["stateLos"] = writer.ToString(); } var los2 = new LosFormatter(true, macKeyModifier); using (var writer = new StringWriter()) { los2.Serialize(writer, person); ViewData["stateLosMac"] = writer.ToString(); } return View(person); } }

ASPX

    <%: ViewData["stateLos"]%>
    <%: ViewData["stateLosMac"]%>
    <%: Html.Serialize("statePlain", Model, SerializationMode.Plaintext)%>
    <%: Html.Serialize("stateSigned", Model, SerializationMode.Signed)%>
    <%: Html.Serialize("stateEncrypted", Model, SerializationMode.Encrypted)%>
    <%: Html.Serialize("stateEncryptedAndSigned", Model, SerializationMode.EncryptedAndSigned)%>

HTML

    /wEywQEAAQAAAP////8BAAAAAAAAAAwCAAAAQFN0YXRlVGVzdCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABdTdGF0ZVRlc3QuTW9kZWxzLlBlcnNvbgIAAAAVPE5hbWU+a19fQmFja2luZ0ZpZWxkFDxBZ2U+a19fQmFja2luZ0ZpZWxkAQAIAgAAAAYDAAAADOODq+ODleOCo+ODvBIAAAAL
    /wEywQEAAQAAAP////8BAAAAAAAAAAwCAAAAQFN0YXRlVGVzdCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABdTdGF0ZVRlc3QuTW9kZWxzLlBlcnNvbgIAAAAVPE5hbWU+a19fQmFja2luZ0ZpZWxkFDxBZ2U+a19fQmFja2luZ0ZpZWxkAQAIAgAAAAYDAAAADOODq+ODleOCo+ODvBIAAAAL
    <input name="statePlain" type="hidden" value="/wEywQEAAQAAAP////8BAAAAAAAAAAwCAAAAQFN0YXRlVGVzdCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABdTdGF0ZVRlc3QuTW9kZWxzLlBlcnNvbgIAAAAVPE5hbWU+a19fQmFja2luZ0ZpZWxkFDxBZ2U+a19fQmFja2luZ0ZpZWxkAQAIAgAAAAYDAAAADOODq+ODleOCo+ODvBIAAAAL" />

    <input name="stateSigned" type="hidden" value="/wEywQEAAQAAAP////8BAAAAAAAAAAwCAAAAQFN0YXRlVGVzdCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABdTdGF0ZVRlc3QuTW9kZWxzLlBlcnNvbgIAAAAVPE5hbWU+a19fQmFja2luZ0ZpZWxkFDxBZ2U+a19fQmFja2luZ0ZpZWxkAQAIAgAAAAYDAAAADOODq+ODleOCo+ODvBIAAAALIrCvmCvq7EIjLblEZK6YIw0F/48=" />
    <input name="stateEncrypted" type="hidden" value="tMKYGlOhnBXLHzreFpfQURQbcha/ZHrcdQbcNs7Vxud0Nq4yQ41FUq1DDqDnORVqxV+VSfpOFHqQFA/ylboLGtpQRbH46a3YJsRqzKf5dlq7tOlp7Ys3zMCWE8ozJ4m2FUG9/bq7KVCo9UsNxOjLWkokHQtr/SORahKAPnbeuFsHOn0B3fl0d1DPtl9MPANWp5qwqqdi+c7JuplYyFT6tEqm3Og+M7fJYnmc3Vkj1oiHclyMCvh6X9Ti9uyPuVvwGpBa2pj6gbETDQa6pQJ4rQ==" />
    <input name="stateEncryptedAndSigned" type="hidden" value="tMKYGlOhnBXLHzreFpfQURQbcha/ZHrcdQbcNs7Vxud0Nq4yQ41FUq1DDqDnORVqxV+VSfpOFHqQFA/ylboLGtpQRbH46a3YJsRqzKf5dlq7tOlp7Ys3zMCWE8ozJ4m2FUG9/bq7KVCo9UsNxOjLWkokHQtr/SORahKAPnbeuFsHOn0B3fl0d1DPtl9MPANWp5qwqqdi+c7JuplYyFT6tEqm3Og+M7fJYnmc3Vkj1oiHclyMCvh6X9Ti9uyPuVvwGpBa2pj6gbETDQa6pQJ4rQ==" />

machineKey

    <machineKey
      validationKey="CABF0BEECF16B90F1BF9B5196A0E12EBBB950D6088FEDC9D2A674D75BE461713A1B9EA89C7B774CA249A45605B64994C54B9F59DA06AC673DE55A4661A7AE6DC"
      decryptionKey="79B6DB1CACB7B81A5EF317F43D5B05BE6872F3C285544222E32F36820785A4E4"
      validation="SHA1" 
      decryption="AES"/>

LosFormatter自体が内部でObjectStateFormatterを呼び出してた(Reflector確認)ので、イケるかな~と思ったんだけどな~。LosFormatterの使い方間違ってますかね?