Encrypt cookies in ASP.NET

后端 未结 2 1868
萌比男神i
萌比男神i 2020-11-28 09:35

I would like to encrypt cookies in ASP.NET.

I have followed the method in this article, but it has the drawback that is uses reflection on an internal method. This h

2条回答
  •  生来不讨喜
    2020-11-28 10:02

    You don't need to roll your own any more.

    .Net 4.5 has MachineKey.Protect() and MachineKey.Unprotect().

    System.Web.Security.MachineKey

    .Net 4.0 has MachineKey.Encode() and MachineKey.Decode(). You should just set the MachineKeyProtection to 'All'. These are now obsolete though and you should use the newer ones if you have 4.5.

    Note if you try and use these in something like a console app instead of ASP.Net it seems to generate a new key with every app restart. I only checked it quickly but in ILSpy it looks like it generates its own defaults if the appropriate app.setting are missing.

    I haven't been able to find a non-ASP.Net equivalent.

提交回复
热议问题