将Google身份验证添加到我的非MVC应用程序

时间:2015-11-22 15:56:19

标签: asp.net authentication oauth google-api asp.net-identity

我有一个asp.net Web窗体应用程序。它不是MVC,这似乎是一个重要的区别。它使用个人身份验证。一切似乎工作得很好,直到调用http://localhost:888/signin-google。然后,没有。我认为应该将某些内容存储到我的AspNetUserLogins表中,并且我应该已经登录到我的网站。

我的问题:显然,我错过了一些东西。我需要修复哪些内容才能让我的用户使用他们的Google ID进行身份验证?

我尝试了什么

  • 我已经创建了一个空白的新ASP.NET Web窗体应用程序 确定我是否更改了任何OAuth / OWin代码。我明白了 同样的问题。
  • 我已将Facebook身份验证添加到我的应用程序中。它 工作良好。点击Login with Facebook按钮。片刻之后,我登录了。
  • Web.config已将authentication modeForms更改为None

我读过的内容

  • 我直接从Visual模板中读到this blog Studio 2015为我生成。所有这一切都是为了评论 UseGoogleAuthentication()我已经完成并填写了 我的客户ID和客户密码。

  • 我也读过this helpful article但无济于事。

  • 我在这里发布了thisthis,甚至是this,但是 使用我的Web窗体应用程序中不存在的MVC构造。

发生了什么

  • 我使用Facebook登录,我的应用程序就像我一样 一个经过验证的用户。我注意到AspNetUserLogins中的Facebook记录。

  • 我使用Google登录,我回复signin-google然后直接回到我的登录页面而没有进行身份验证。

  • 我追踪我的代码,调用了GetExternalLoginInfo() Page_Load的{​​{1}}方法正在返回RegisterExternalLogin.aspx。我已经阅读了Google搜索中的几篇文章 " GetExternalLoginInfo returns null"的结果再无济于事。

  • 如果我直接在我的null输入http://localhost:888/signin-google 浏览器,我得到一个空白页面。不是404,只是一个空白页面。我看到了 我的浏览器历史记录,包含对signin-google的引用 证明此页面涉及回调(例如state= querystring params)。

我在服务器日志中看到以下条目(为清晰起见而编辑)

2015-11-22 06:25:12 ::1 GET /signin-google state=***&code=****&authuser=0&session_state=****&prompt=none 888 - ::1 Mozilla/5.0+...+Safari/537.36 http://localhost:888/Account/Login 302 0 0 228
2015-11-22 06:25:12 ::1 GET /Account/RegisterExternalLogin providerName=Google&returnUrl=&error=access_denied 888 - ::1 Mozilla/5.0+...+Safari/537.36 http://localhost:888/Account/Login 302 0 0 112

我的代码

在Startup.Auth.cs中,添加到方法ConfigureAuth

GoogleOAuth2AuthenticationOptions googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions()
{
    ClientId = ConfigurationManager.AppSettings["Google.ClientId" ],
    ClientSecret = ConfigurationManager.AppSettings["Google.ClientSecret"],

    // the rest of this per https://stackoverflow.com/questions/21964139/googleoauth2-issue-getting-internal-server-500-error/23595862#23595862
    //CallbackPath = new PathString( "/Callbacks/Google" ),

    Provider = new GoogleOAuth2AuthenticationProvider
    {
        OnAuthenticated = (context) =>
        {
            context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
            context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
            return Task.FromResult(0);
        }
    }
};

googleAuthenticationOptions.Scope.Add("email");

app.UseGoogleAuthentication(googleAuthenticationOptions);

0 个答案:

没有答案