使用Identity

时间:2016-10-29 16:23:55

标签: c# asp.net-mvc asp.net-identity

我正在尝试使用this article在ASP.NET MVC应用程序中实现Google登录,但是一旦我登录,我就注意到了Request.IsAuthenticated = false。这是我的代码。这几乎与我用于Facebook和Facebook登录的代码相同。

       const string GoogleClaimBaseName = "urn:google:{0}"; 
       string GoogleClaimProfile = String.Format(GoogleClaimBaseName, "profile");
       string GoogleClaimSub = String.Format(GoogleClaimBaseName, "sub");
       string GoogleClaimName = String.Format(GoogleClaimBaseName, "name");       
       string googleClientId = "Id";

        string googleClientSecret = "Secret";

        var googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions
        {
            ClientId = googleClientId,
            ClientSecret = googleClientSecret,

            Provider = new GoogleOAuth2AuthenticationProvider()
            {
                OnAuthenticated = async googleContext =>
                {
                    string profileClaimName = string.Format(GoogleClaimBaseName, "profile");

                    foreach (JProperty property in googleContext.User.Children())
                    {
                        var claimType = string.Format(GoogleClaimBaseName, property.Name);
                        string claimValue = (string)property.Value;
                        if (!googleContext.Identity.HasClaim(claimType, claimValue))
                            googleContext.Identity.AddClaim(new Claim(claimType, claimValue, xmlSchemaString, "External"));
                    }
                }
            }
        };
        googleAuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
        googleAuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
        app.UseGoogleAuthentication(googleAuthenticationOptions);

当我尝试获取Google的个人资料信息时,问题就出现了。在此之前,一切都运转正常。

0 个答案:

没有答案