使用ASP.NET MVC外部身份验证导入Gmail联系人

时间:2016-09-26 13:41:01

标签: c# asp.net-mvc gmail-api google-authentication

我正在使用带有SQL SERVER 2008 R2的ASP.NET MVC 4.5.2,并且我已经在我的Google应用程序中使用ASP.NET Auth Integration外部登录功能应用了相同的代码示例。

我刚刚使用Gmail完成了登录的编码,但在允许访问权限后,我无法从特定用户的Gmail帐户中提取/获取联系人。

我已经提到了this文章,并希望尝试使用它,但我不需要获取密码才能获得他们的联系人,我怎么能这样做?

在允许代码之前:

    public void ConfigureAuth(IAppBuilder app)
            {
    app.CreatePerOwinContext(ApplicationDbContext.Create);
                app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
                app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
                app.UseCookieAuthentication(new CookieAuthenticationOptions());
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
var googleOptions = new GoogleOAuth2AuthenticationOptions()
            {
                ClientId = "my client id",
                ClientSecret = "client secret",
Provider = new GoogleOAuth2AuthenticationProvider()
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new System.Security.Claims.Claim("GoogleAccessToken"
                            , context.AccessToken));
                        foreach (var claim in context.User)
                        {
                            var claimType = string.Format("urn:google:{0}", claim.Key);
                            string claimValue = claim.Value.ToString();
                            if (!context.Identity.HasClaim(claimType, claimValue))
                                context.Identity.AddClaim(new System.Security.Claims.Claim(claimType,
                                    claimValue, "xmlSchemaString", "Google"));
                        }
                    }
                }
};
            googleOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
            googleOptions.Scope.Add("email");
            googleOptions.Scope.Add("https://www.googleapis.com/auth/contacts.readonly");
            googleOptions.Scope.Add("https://www.googleapis.com/auth/user.addresses.read");
            googleOptions.Scope.Add("https://www.googleapis.com/auth/user.birthday.read");
            app.UseGoogleAuthentication(googleOptions);
}

允许代码后:

 [AllowAnonymous]
            public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
            {
                var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (loginInfo == null)
                {
                    return RedirectToAction("Login");
                }

                // Sign in the user with this external login provider if the user already has a login
                var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
                switch (result)
                {
                    case SignInStatus.Success:
                        return RedirectToLocal(returnUrl);
                    case SignInStatus.LockedOut:
                        return View("Lockout");
                    case SignInStatus.RequiresVerification:
                        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                    case SignInStatus.Failure:
                    default:
                        ExternalLoginConfirmationViewModel data = new ExternalLoginConfirmationViewModel();
                        data.ExUsername = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == ClaimTypes.Name).
                            Select(c => c.Value).SingleOrDefault();
                        data.ExFirstName = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == ClaimTypes.GivenName).
                            Select(c => c.Value).SingleOrDefault();
                        data.ExLastName = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == ClaimTypes.Surname).
                            Select(c => c.Value).SingleOrDefault();
                        data.Email = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == ClaimTypes.Email).
                            Select(c => c.Value).SingleOrDefault();
                        // If the user does not have an account, then prompt the user to create an account
                        ViewBag.ReturnUrl = returnUrl;
                        ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                }
            }

请有人帮我解决这个问题!

任何帮助都将不胜感激。

0 个答案:

没有答案