【问题标题】:sharing asp.net 4 forms authentication cookie with asp.net core 2.0与 asp.net core 2.0 共享 asp.net 4 表单身份验证 cookie
【发布时间】:2018-02-17 12:39:35
【问题描述】:

我们在 IIS 中设置了多个应用程序,其中一个应用程序处理所有应用程序的登录。此应用程序是一个 asp.net 4 站点并使用表单身份验证 cookie。

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" protection="All" cookieless="UseCookies" path="/" name="CookieName" />
</authentication>

我们可以通过 owin 成功使用此 cookie 登录 asp.net 4.5 应用程序。

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            TicketDataFormat = new SharedTicketDataFormat(),
            CookieName = "CookieName",
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });


public class SharedTicketDataFormat : ISecureDataFormat<AuthenticationTicket>
{
    public string Protect(AuthenticationTicket data)
    {
        return FormsAuthentication.Encrypt(new FormsAuthenticationTicket(data.Identity.Name, false, -1));
    }
    public AuthenticationTicket Unprotect(string protectedText)
    {
        var ticket = FormsAuthentication.Decrypt(protectedText);
        var identity = new FormsIdentity(ticket);
        return new AuthenticationTicket(identity, new AuthenticationProperties());
    }
}

在 asp.net core 2.0 中我不知道要连接应用程序以使用共享 cookie

在 Startup.cs 中 配置

app.UseAuthentication();

配置服务

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie(options =>
        {
            options.Cookie.Name = "CookieName";
        });

【问题讨论】:

  • 嗨,你能解决这个问题吗?我也在寻找类似的解决方案。
  • @SamJackSon 不,很遗憾没有,由于时间压力,我们回到了 asp.net 4.6。
  • 感谢@skyfoot 的回复。我尝试使用此解决方案,并且初步结果很好,似乎可以正常工作。更多地使用 AuthorizationHandler。 github.com/dazinator/AspNetCore.LegacyAuthCookieCompat

标签: asp.net cookies forms-authentication asp.net-core-2.0


【解决方案1】:

我的理解是,您需要从依赖机器密钥进行 cookie 加密转变为使用 DataProtectionProvider。文档中的这篇文章非常清楚地说明了一切:

https://docs.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-3.1#share-authentication-cookies-with-aspnet-core-identity

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2019-04-19
    • 2014-04-01
    • 2011-08-09
    相关资源
    最近更新 更多