【问题标题】:Asp.net Core Identity 2.0 Google LogoutAsp.net Core Identity 2.0 谷歌注销
【发布时间】:2017-09-08 12:26:08
【问题描述】:

我已经开始研究 Google 登录并添加了正常的提供商。

ddGoogle(go =>
            {
                go.ClientId = "xxxxx";
                go.ClientSecret = "-xxxxx";
                go.SignInScheme = IdentityConstants.ExternalScheme;
            });

我的测试方法只是为了开始它看起来像这样

public ActionResult TestGoogle()
{
    var redirectUrl = Url.Action(nameof(ExternalCallback), "Account", new { ReturnUrl = "" });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);
    return Challenge(properties, "Google");
}

一切顺利,我去 google 登录并按预期获得所有必需声明的重定向。

问题是当我打电话给_signInManager.SignOutAsync() 时,它似乎没有做任何事情。没有错误,但是当我返回到我的 TestGoogle 操作时,我会使用所有凭据重定向到回调。

我缺少什么吗?

【问题讨论】:

    标签: c# asp.net-core google-signin asp.net-core-identity


    【解决方案1】:

    在您的注销操作中添加此代码

    return Redirect("@987654321@");
    

    【讨论】:

      【解决方案2】:

      这就是我配置代码的方式:

      配置 2 个 Cookie,一个 (MainCookie) 用于本地登录,第二个 (ExternalCookie) 用于 google。

      services.AddAuthentication("MainCookie").AddCookie("MainCookie", options =>
              {
      
              });
      
      services.AddAuthentication("ExternalCookie").AddCookie("ExternalCookie", o =>
              {
      
              });
      

      如下图配置google认证:

        services.AddAuthentication(
                  v =>
                  {
                      v.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                      v.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                  }).
      
                  AddGoogle("Google", googleOptions =>
               {
                   googleOptions.ClientId = "xxx...";
                   googleOptions.ClientSecret = "zzz...";
                   googleOptions.SignInScheme = "ExternalCookie";
                   googleOptions.Events = new OAuthEvents
                   {
                       OnRedirectToAuthorizationEndpoint = context =>
                       {
                           context.Response.Redirect(context.RedirectUri + "&hd=" + System.Net.WebUtility.UrlEncode("gmail.com"));
      
                           return Task.CompletedTask;
                       }
                   };
       });
      

      TestGoogle() 方法会将您重定向到 google 登录页面。

      然后您可以像这样从 google 获取声明:

       public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
          {
             var info = await HttpContext.AuthenticateAsync("ExternalCookie");
      
              //Sign in to local cookie and logout of external cookie
              await HttpContext.SignInAsync("MainCookie", info.Principal);
              await HttpContext.SignOutAsync("ExternalCookie");
              //ExternalCookie will be deleted at this point. 
              return RedirectToLocal(returnUrl);
          }
      

      如果你现在想验证任何方法,你可以这样做:

           [Authorize(AuthenticationSchemes = "MainCookie")]
           public async Task<IActionResult> Contact()
          {
             //Only authenticated users are allowed.
          }
      

      【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2015-02-09
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多