ASP.NET MVC 5 ExternalLogin()方法无法为Google OAuth执行;适用于Facebook

时间:2015-10-19 19:11:03

标签: c# asp.net-mvc oauth google-oauth

我正在使用ASP.NET MVC 5的内置OAuth代码,允许用户使用他们的Google或Facebook凭据配置成员资格,所需的行为是,一旦访问令牌通过,用户就会被重定向到应用程序主页通过Home{Controller}\Index{action}。关于谷歌,我在执行流程方面遇到了麻烦,尽管它与Facebook的逻辑完全相同,就像魅力一样(见下文):

1。用户点击登录按钮:

 using (Html.BeginForm("ExternalLogin", "Account", new {ReturnUrl = Model.ReturnUrl}))
    {
        @Html.AntiForgeryToken()

        <div class="social_sign">
            @foreach (AuthenticationDescription p in loginProviders)
            {
                <button class="@p.Caption"
                        id="@p.AuthenticationType"
                        name="provider"
                        title="Log in using your @p.Caption account"
                        type="submit"
                        value="@p.AuthenticationType">
                    <i class="fa fa-@p.Caption" style="color: #ffffff"></i>
                </button>
            }
        </div>
    }

2。 ExternalLogin()方法被称为:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new {ReturnUrl = returnUrl}));
    }

第3。调用ExternalLoginCallback()方法:

  [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:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
    }

4。调用Login()方法:

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

但是,当用户点击Google按钮时,会调用ExternalLogin()方法然后执行停止(即应用尝试返回不存在的Account/ExternalLogin视图),而不是加载{{ 1}}错误,页面加载只是在Chrome中超时。在方法中的Resource not found处设置断点表明它没有被执行。我没有看到如何发生这种情况,因为我无法使用与不同登录提供程序完全相同的逻辑重现错误。我刚接触ASP.NET MVC的OAuth配置。

return new ChallengeResult()中的代码看起来也很好:

StartupAuth.cshtml

我错过了什么?

2 个答案:

答案 0 :(得分:1)

AuthenticationManager.GetExternalLoginInfoAsync()返回null的最常见原因是Google API未在Google开发者控制台中启用。默认情况下不启用它,当您第一次尝试使用MVC5模板中包含的外部登录提供程序时,这可能会产生误导。

要检查是否已启用,请转到https://console.developers.google.com并选择您已创建的项目。

导航到API,并在社交媒体API部分下查找Google+。如果您看到启用API的按钮,则会为此应用程序禁用Google+ API,点击启用可以解决您的问题。

答案 1 :(得分:0)

添加&#34;删除&#34; Web.config下面的行为我做了。

<system.webServer>
  <modules>
    <remove name="FormsAuthentication" />
  </modules>
<system.webServer>