【问题标题】:Can ASP.NET Identity cookies be backwards compatible with Forms Authentication?ASP.NET 身份 cookie 可以与表单身份验证向后兼容吗?
【发布时间】: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


    【解决方案1】:

    This discussion GitHub 上的几个 ASP.NET Identity 和 IdentityServer 开发人员对我很有帮助。我关于在表单身份验证和 ASP.NET 身份之间共享 cookie 的问题的简短回答是“你疯了!”幸运的是,还有一些替代方案。

    Brock Allen 将我指向IdentityServer's GitHub repo for client examples。对我来说,我认为MvcFormPostClient example 会成功。实际上,在没有 OWIN 或 ASP.NET Identity 的旧 MVC 应用程序中实现 OpenID Connect 并不难。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 2011-08-19
      • 2012-08-01
      • 2017-10-26
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多