【问题标题】:ASP.NET Identity / OData using CORS and cookie authentication missing Auth cookieASP.NET Identity / OData 使用 CORS 和 cookie 身份验证缺少 Auth cookie
【发布时间】:2015-10-01 04:56:23
【问题描述】:

我有一个 ASP.NET Identity 站点和一个 ASP.NET OData 站点。
两个站点都启用了 CORS,并且两个站点都使用 ASP.NET Identity CookieAuthentication。
当我使用 IIS(非 express)在我的计算机上本地执行这两个站点时,AUTH cookie 将在每个请求的标头中传递到 OData 站点。
但是,当我将站点部署到生产 IIS 服务器时,调用生产 OData 站点时标头会丢失 AUTH cookie。
生产和我的本地 IIS 都具有相同的域名,并且 CORS 设置为允许所有。

WebApiConfig 有

cors = new Http.Cors.EnableCorsAttribute("*", "*", "*");
config.Enable(cors);

在任何人问之前,是的,站点之间的机器密钥是相同的。

更新
这似乎是一个 CORS 问题。
当两个站点都在我的本地计算机上时,它们使用相同的主机名和域名,但是当站点在生产服务器上时,它们具有不同的主机名和相同的域名。

【问题讨论】:

    标签: cookies asp.net-web-api cors odata asp.net-identity


    【解决方案1】:

    您可能需要在 OAuthAuthorizationServerProvider 中指定“Access-Control-Allow-Origin”。

    我正在使用 OWIN,但您应该可以做类似的事情。

    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
    

    【讨论】:

      【解决方案2】:

      尝试在您的 OWIN 启动类中添加策略,如下所示。请记住,Startup 类可能有一些不同的类文件,因为它是一个部分类。此外,检查 ConfigureAuth 方法以查看是否根据您的需要设置了所有内容。例如,您在 ConfigureAuth 方法中将 Identity 的外部登录 cookie 设置为如下所示,以允许 facebook 和 google 等外部登录 cookie。

          public void Configuration(IAppBuilder app)
          {
              // 
              app.UseCors(CorsOptions.AllowAll);
              ConfigureAuth(app);
      
          }
      
      
              app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
      

      【讨论】:

        【解决方案3】:

        我终于让它工作了。
        在 ASP.NET Identity 站点中,我有以下内容:

        // configure OAuth for login
        app.UseCookieAuthentication(new CookieAuthenticationOptions {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                Provider = new CookieAuthenticationProvider(),
                LoginPath = new PathString("/Account/Login.aspx"),
                CookieName = ".TESTAUTH",
                CookieDomain = ".test.com",
                CookieSecure = CookieSecureOption.Always
            });
        

        似乎 ASP.NET Identity 站点上的重要部分是“CookieName、CookieDomain 和机器密钥”必须与 OData 站点上的匹配。

        然后在 OData 网站上我有以下内容:

        // configure OAuth for login
        app.UseCookieAuthentication(new CookieAuthenticationOptions {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                Provider = new CookieAuthenticationProvider { OnApplyRedirect = ApplyRedirect },
                LoginPath = new PathString("/Account/Login.aspx"),
                CookieName = ".TESTAUTH",
                CookieDomain = ".test.com",
                CookieSecure = CookieSecureOption.Always
            });
        
        // build the configuration for web api
        HttpConfiguration config = new HttpConfiguration();
        
        // Enable CORS (Cross-Origin Resource Sharing) for JavaScript / AJAX calls
        // NOTE: USING ALL "*" IS NOT RECOMMENDED
        var cors = new Http.Cors.EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);
        
        // call the web api startup
        WebApiConfig.Register(config);
        app.UseWebApi(config);
        
        private void ApplyRedirect(CookieApplyRedirectContext context)
        {
            Uri absoluteUri = null;
            if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, absoluteUri)) 
            {
                var path = PathString.FromUriComponent(absoluteUri);
                if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath) 
                {
                    QueryString returnURI = new QueryString(context.Options.ReturnUrlParameter, context.Request.Uri.AbsoluteUri);
                    context.RedirectUri = "https://www.test.com/Account/Login.aspx" + returnURI.ToString;
                }
            }
            context.Response.Redirect(context.RedirectUri);
        }
        

        “LoginPath”是必需的,即使它在 OData 站点上不存在并且您不能使用另一个站点的完整 URL 作为登录路径。
        我使用“OnApplyRedirect”重定向到实际的登录页面。

        我不确定“config.EnableCors”和“app.UseCors”之间有什么区别,但 EnableCors 似乎现在可以工作。

        【讨论】:

          猜你喜欢
          • 2022-01-23
          • 2012-10-16
          • 1970-01-01
          • 2018-12-30
          • 2018-05-10
          • 1970-01-01
          • 2015-01-20
          • 2017-10-29
          • 2018-03-10
          相关资源
          最近更新 更多