【发布时间】:2018-02-07 06:10:16
【问题描述】:
我有许多现有的 ASP.NET MVC Web 应用程序,它们都使用 ASP.NET 成员资格和表单身份验证,不使用 OWIN,但我希望在 .NET Core 中开发新应用程序1.1 使用 IdentityServer4 进行身份验证。我有一个使用自定义用户存储实现的 IdentityServer,因此旧 ASP.NET Membership 数据库中的现有用户可以登录到我的 IdentityServer(有关更多信息,请参阅this StackOverflow 问题),但是由 ASP.NET Identity 创建的身份验证 cookie使用表单身份验证的旧应用无法识别。
我希望使用 ASP.NET Identity 在较旧的 Forms 身份验证应用和较新的应用之间进行单点登录,最好对较旧的应用进行尽可能少的更改。有没有办法让 ASP.NET Identity 生成 Forms Authentication 可以理解的身份验证 cookie?
我认为在这两种情况下,cookie 的内容都是序列化和加密的ClaimsPrincipal,所以我的最大障碍似乎是让 Forms Authentication 应用程序和 ASP.NET Identity 应用程序位于同一页面上关于加密/解密 cookie。由于 Forms Authentication 使用 web.config 文件中的 <machineKey> 元素加密 cookie,而 .NET Core 中的 ASP.NET Identity 使用 DPAPI,我必须弄清楚如何弥合这一差距。也许是这样的?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<IdentityOptions>(options =>
{
...
options.Cookies.ApplicationCookie.DataProtectionProvider =
new FormsAuthenticationDataProtector();
...
});
}
FormsAuthenticationDataProtector.cs
public class FormsAuthenticationDataProtector : IDataProtector
{
public IDataProtector CreateProtector(string purpose)
{
return this;
}
public byte[] Protect(byte[] plaintext)
{
// TODO: Mimic the Forms Authentication encryption algorithm.
}
public byte[] Unprotect(byte[] protectedData)
{
// TODO: Mimic the Forms Authentication decryption algorithm.
}
}
但我不知道Protect() 和Unprotect() 方法的主体应该是什么。
【问题讨论】:
标签: c# asp.net cookies asp.net-identity forms-authentication