开发者

ASP.NET MVC 3 AntiForgeryToken and custom MachineKey configuration

We've run into some issues with ASP.NET MVC 3 AntiForgeryTok开发者_JS百科en HTML helper when having a custom configured MachineKey in Web.Config. The error is easy to reproduce if you change your MachineKey to the following (taken from Microsoft HowTo-guide on how to configure machine key).

<machineKey  
    validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7
           AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B"           
    decryptionKey="ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F"
    validation="SHA1"
    decryption="AES"/>

The exception thrown by AntiForgeryToken is as follow:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   System.Web.Configuration.MachineKeySection.SetInnerOuterKeys(Byte[] validationKey, Byte[]& inner, Byte[]& outer) +11499173
   System.Web.Configuration.MachineKeySection.ConfigureEncryptionObject() +228
   System.Web.Configuration.MachineKeySection.EnsureConfig() +287
   System.Web.Configuration.MachineKeySection.HashData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length) +46
   System.Web.Security.MachineKey.Encode(Byte[] data, MachineKeyProtection protectionOption) +58
   System.Web.Helpers.AntiForgeryDataSerializer.<.ctor>b__2(Byte[] bytes) +13
   System.Web.Helpers.AntiForgeryDataSerializer.Serialize(AntiForgeryData token) +365
   System.Web.Helpers.AntiForgeryWorker.GetAntiForgeryTokenAndSetCookie(HttpContextBase httpContext, String salt, String domain, String path) +326
   System.Web.Helpers.AntiForgeryWorker.GetHtml(HttpContextBase httpContext, String salt, String domain, String path) +28
   System.Web.Helpers.AntiForgery.GetHtml(HttpContextBase httpContext, String salt, String domain, String path) +75
   System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path) +48

Is this a bug in the ASP.NET MVC 3 Html Helper to generate the AntiForgeryToken? Or am I missing something in regards to configuring machine keys?


It looks like I screwed up the keys - using http://aspnetresources.com/tools/machineKey I was able to generate a valid machineKey config section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜