EmulateFormsAuthentication 0.2.0

dotnet add package EmulateFormsAuthentication --version 0.2.0
                    
NuGet\Install-Package EmulateFormsAuthentication -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EmulateFormsAuthentication" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EmulateFormsAuthentication" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="EmulateFormsAuthentication" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EmulateFormsAuthentication --version 0.2.0
                    
#r "nuget: EmulateFormsAuthentication, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EmulateFormsAuthentication@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EmulateFormsAuthentication&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=EmulateFormsAuthentication&version=0.2.0
                    
Install as a Cake Tool

EmulateFormsAuthentication Prject

FormsAuthentication 是微软在.Net2时推的一套WEB程序的验证机制,会把用户的标识信息加密后存储在浏览器的cookies中。但微软在.net core支持跨平台后在 System.Web.Security NS下就已经不两万提供此类了。 但从2.0或4.0过来的项目肯定还是要考虑兼容性问题。还好微软开源了.net4.8的源码在里面可以看到实现的方式.

模拟类库实现原理

Cookies 当中存储的是 FormsAuthenticationTicket 这个成员对象串行化后的数据,当中会包含版本、过期时间、标识等信息。串行的方法是会把票据序列化版本号写第一位,当前为0x01 然后是ticket的内容。最后存储一个0xff 用来标记数据区的结束。 串行化数据后就是做一个Hash操作,支持的Hash有[sha1/md5/sha256/sha384/sha512],会把HASH的结果追加到数据串后后面。

接下来我们就开始准备对我们的数据进行加密了,这里支持的对称加密算法[AES/DES/3DES]。加密完成后还会对数据串再进行一个HASH并填充到数据串的末尾。最后整个数据串的格式如下

Crypto(Serialize(ticket) + hash(Serialize(ticket))) + hash()

解密就是反向操作并进行HASH验证即可。

这次我们直接用最新的.Net 6来进行实现。

使用

当前已发布的NuGet版本为 0.1.0 直接安装即可。

dotnet add package EmulateFormsAuthentication --version 0.1.0

当前版本 对于加密仅支持 3DES,对于 hash 仅支持 Sha1。

FormsAuthentication formsAuthentication = new FormsAuthentication(decryptionKey, validationKey);

FormsAuthenticationTicket formsAuthenticationTicket = new FormsAuthenticationTicket(1, "username", new DateTime(), new DateTime(2055, 1, 1), false, "User Data", "/");
// 加密
string encryptData = formsAuthentication.Encrypt(formsAuthenticationTicket);
// 解密
FormsAuthenticationTicket formsAuthenticationTicket1 = formsAuthentication.Decrypt(encryptData);

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 382 7/23/2022
0.1.0 323 7/23/2022