【问题标题】:MVC 5 OAuth with CORS带有 CORS 的 MVC 5 OAuth
【发布时间】:2015-02-06 12:19:18
【问题描述】:

我阅读了几篇谈论一些类似问题的帖子,但我还没有这样做。

我正在对“Account/ExternalLogin”执行 ajax,它会生成 ChallengeResult 并开始使用 OWIN 进行身份验证。

这是我的Startup 课程:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {            
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        app.UseCors(CorsOptions.AllowAll);
        var goath2 = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
        {
            ClientId = "myclientid",
            ClientSecret = "mysecret",
            Provider = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationProvider
            {
                OnApplyRedirect = context =>
                {
                    string redirect = context.RedirectUri;

                    const string Origin = "Origin";
                    const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";

                    // origin is https://localhost:44301                        
                    var origin = context.Request.Headers.GetValues(Origin).First();


                    // header is present
                    var headerIsPresent = context.Response.Headers.ContainsKey(AccessControlAllowOrigin);
                    context.Response.Redirect(redirect);                        
                }
            }
        };

        app.UseGoogleAuthentication(goath2);
    }
}

我正在通过 app.UserCors(CorsOptinos.AllowAll); 行启用 CORS 支持 而且我知道标头已添加到响应中,因为我拦截了OnApplyRedirectevent,当我查找来源时,它被设置为“localhost:443001”并且标头“Access-Control-Allow-Origin”也被设置到这个值。

但是,当响应发送到客户端时,我遇到以下错误:

XMLHttpRequest 无法加载 https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxx 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点https://localhost:44301 访问。

我在这里缺少什么。

我可以“手动”解决所有这些问题(直接从客户端请求谷歌...),但我真的想使用 OWIN 中间件。

【问题讨论】:

    标签: ajax asp.net-mvc oauth-2.0 cors google-oauth


    【解决方案1】:

    您正在从https://localhost:44301 域向 google 发出请求。为了工作,'Access-Control-Allow-Origin' 应该在列表中有'https://localhost:44301' 源域。所以在这种情况下,谷歌需要在“Access-Control-Allow-Origin”中设置这个域。

    查看您收到的回复,似乎 google 不允许来自您的域的跨源请求。为了解决这个问题,我认为您需要在 google https://developers.google.com 上注册您的域。

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2018-10-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2019-02-11
      相关资源
      最近更新 更多