【问题标题】:ASP.NET MVC Authentication using External Provider - Google/Facebook使用外部提供程序的 ASP.NET MVC 身份验证 - Google/Facebook
【发布时间】:2016-05-26 23:00:03
【问题描述】:

我在我的 asp.net MVC 应用程序中使用 Microsoft/Google/Facebook 身份验证,它对用户进行身份验证并将用户重定向到我的网站。这很好用。

问题:任何拥有 Microsoft/Google/Facebook 帐户的人都可以登录我的应用程序。我应该只允许那些在我们的数据库中注册/映射的用户,即如果一个人购买了许可证,只有他应该能够使用外部提供商登录。

示例: 1) User1 有一个 Microsoft/Google 帐户,并且 user1 是我们数据库中的有效用户。所以我们可以让他看到我们网站上的内容。 2) user2 有一个 microsoft/Google 帐户,但他不是我们数据库中的有效用户。他应该无法访问我们的网站。

如何在 ASP.NET MVC 中实现这一点。我正在使用来自外部提供商的客户端 ID 和客户端密钥。

启动类的示例代码

  app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
    ClientId = "",
    ClientSecret = ""
    });

【问题讨论】:

    标签: asp.net-mvc authentication oauth-2.0


    【解决方案1】:

    在 AccountController.cs 中(如果您没有更改默认代码)

     [AllowAnonymous]
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }
    
            if(EmailNotRegistered(result.ExtraData["userid"]))
            {
                FormsAuthentication.SignOut();
                return RedirectToAction("ExternalLoginFailure");
            }
    
            var bresult = OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false);
            if (bresult)
            {
                return RedirectToLocal(returnUrl);
            }
    
           // etc...
        }
    

    您需要编写函数bool EmailNotRegistered(string email) 并执行检查本地数据库的逻辑。 Membership API 中可能已经有一些东西可以检查,但我现在不知道。

    顺便说一下,每个提供者都不同,因此 ExtraData 字段可能是“电子邮件”或其他内容 - 请使用调试器找出答案!

    【讨论】:

      【解决方案2】:

      这是我的 AccountController

      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 });
                  }
              }
      

      【讨论】:

      • 那你就有答案了! // If the user does not have an account 当前标记您通常允许用户注册本地帐户的位置。更改它以显示权限被拒绝或其他。
      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 2013-07-30
      • 2014-09-28
      相关资源
      最近更新 更多