【问题标题】:Variable cookie path with ASP.NET Identity带有 ASP.NET 标识的可变 cookie 路径
【发布时间】:2015-04-08 14:50:25
【问题描述】:

我们将多租户 MVC 应用程序从 ASP.NET Membership Provider 迁移到 ASP.NET Identity。

这是我的 Startup.Auth.cs(简化版):

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, Identity, int>(
                        TimeSpan.FromMinutes(30),
                        (manager, user) =>
                            manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
                        clIdentity => clIdentity.GetUserId<int>())
            }
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

在我们的多租户应用程序中,每个租户都有自己的“slug”(例如http://example.com/tenant1/http://example.com/tenant2/

但是,目前,cookie 存储在根目录中。这会导致安全问题,因为租户 1 的用户会自动从租户 2 登录网站。

我们如何使 CookiePath(在 CookieAuthenticationOptions 中)变量,以便它根据租户而改变?

【问题讨论】:

    标签: cookies asp.net-identity owin


    【解决方案1】:

    dampee 的大力帮助下,我解决了这个问题。

    CookieAuthenticationOptions 对象中的 CookiePath 只评估一次:在应用程序启动时。 最简单的解决方案(解决方法)是创建一个派生的 CookieAuthenticationProvider 来覆盖 ResponseSignInResponseSignOut。 它们都有一个名为 context 的参数,该参数具有一个名为 CookiePath 的属性。在这两种方法中修改此属性以更改 CookiePath。 你也可以use the class I created

    那么你所要做的就是将CookieAuthenticationOptions中的CookieAuthenticationProvider替换成你刚刚创建的那个。

    这适用于 ApplicationCookie。 ExternalSignInCookie 无关紧要,因为它只是在使用外部登录名登录时临时使用。

    【讨论】:

      【解决方案2】:

      改进了 SamuelDebruyn 自己的解决方案,我发现您可以使用 AuthenticationProperties 对象将 SignIn 调用中的路径传递给提供程序。这样,您可以从源显式传递它,而不是从请求上下文中提取路径作为他的要点:

      // method inside web api controller
      private void SignIn(string name, string cookiePath)
      {
          var claims = new[] { new Claim(ClaimTypes.Name, name) };
          var identity = new ClaimsIdentity(claims, "ApplicationCookie");
      
          var options = new AuthenticationProperties();
          options.Dictionary["CustomCookiePath"] = cookiePath;
      
          var authManager = Request.GetOwinContext().Authentication;
          authManager.SignIn(options, identity);
      }
      
      // Startup.cs
      app.UseCookieAuthentication(new CookieAuthenticationOptions
      {
          Provider = new CustomCookieProvider()
      });
      
      // custom provider
      public class CustomCookieProvider : CookieAuthenticationProvider
      {
          public override void ResponseSignIn(CookieResponseSignInContext context)
          {
              context.CookieOptions.Path = context.Properties.Dictionary["CustomCookiePath"];
              base.ResponseSignIn(context);
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用自定义 ICookieManager 根据请求中的内容动态地将 cookie 值返回到 CookieAuthenticationProvider,为此您仍将 CookiePath 保持为“/”,然后将其留给ICookieManager 可以根据需要返回(或写入)cookie。 CookieManagerCookieAuthenticationOptions 的一个选项。我在这里写了一篇博客:http://shazwazza.com/post/owin-cookie-authentication-with-variable-cookie-paths/

        【讨论】:

          猜你喜欢
          • 2018-02-03
          • 2019-10-03
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 2014-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多