ASP.Net 身份和 Google 身份验证问题

ASP.Net Identity and Google Authentication Issue

我已经在 Google 中设置了项目,它给了我 appid 和 secret

我把id和secret移到了StartUp.Auth

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            app.CreatePerOwinContext<IdentityTestingDbContext>(IdentityTestingDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
            });

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


            app.UseGoogleAuthentication(
                clientId: "*********************.apps.googleusercontent.com ",
                clientSecret: "**************");


        }
    }

以下是外部登录的操作,我正在关注身份示例应用程序(安装包 Microsoft.AspNet.Identity.Samples -Pre)。

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult ExternalLogin(string provider, string returnUrl)
        {
            // Request a redirect to the external login provider
            var challenge = new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
            return challenge;
        }
        // Used for XSRF protection when adding external logins
        private const string XsrfKey = "XsrfId";
        internal class ChallengeResult : HttpUnauthorizedResult
        {
            public ChallengeResult(string provider, string redirectUri)
                : this(provider, redirectUri, null)
            {
            }

            public ChallengeResult(string provider, string redirectUri, string userId)
            {
                LoginProvider = provider;
                RedirectUri = redirectUri;
                UserId = userId;
            }

            public string LoginProvider { get; set; }
            public string RedirectUri { get; set; }
            public string UserId { get; set; }

            public override void ExecuteResult(ControllerContext context)
            {
                var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
                if (UserId != null)
                {
                    properties.Dictionary[XsrfKey] = UserId;
                }
                context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
            }
        }

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

            var user = await UserManager.FindAsync(loginInfo.Login);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    Email = loginInfo.Email,
                    UserName = loginInfo.DefaultUserName,
                    FirstName = string.Empty,
                    LastName = string.Empty
                };

                var result = await UserManager.CreateAsync(user);
                if (!result.Succeeded)
                {
                    return View("Error", result.Errors);
                }

                result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
                if (!result.Succeeded)
                {
                    return View("Error", result.Errors);
                }
            }

            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            identity.AddClaims(loginInfo.ExternalIdentity.Claims);
            AuthenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = false
            }, identity);
            return Redirect(returnUrl ?? "/");
        }

我被重定向到 google,但我在这里遇到错误。看起来我错过了一些东西,但无法弄清楚。我已经搜索了将近 3 个小时,但找不到任何可以帮助解决此问题的方法。

  1. 你看到我做错了什么吗?
  2. 为什么下图中的重定向url是http://localhost:58286/signin-google

以下帮助

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

修复 1: 对于上述问题线程中的 google 设置屏幕截图,授权重定向 url 需要 http://localhost:58286/signin-google。这不是帐户控制器中的回调方法。

修复 2: 我还需要启用 Google+ API,而我在设置期间没有启用