C# Asp.net核心标识2.0谷歌注销
我已经开始研究谷歌登录,并添加了正常的供应商C# Asp.net核心标识2.0谷歌注销,c#,asp.net-core,google-signin,asp.net-core-identity,C#,Asp.net Core,Google Signin,Asp.net Core Identity,我已经开始研究谷歌登录,并添加了正常的供应商 ddGoogle(go => { go.ClientId = "xxxxx"; go.ClientSecret = "-xxxxx"; go.SignInScheme = IdentityConstants.ExternalScheme; }); 我刚刚开始的测试方法如下所示 public Actio
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");
}
一切都很好,我去谷歌登录,并得到重定向与所有必要的索赔预期
问题是当我调用\u signInManager.SignOutAsync()
时,它似乎什么都不做。没有错误,但是当我返回到TestGoogle操作时,我会被重定向到回调,并带有所有凭据
我缺少什么?这是我如何配置代码的: 配置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;
}
};
});
[Authorize(AuthenticationSchemes = "MainCookie")]
public async Task<IActionResult> Contact()
{
//Only authenticated users are allowed.
}
TestGoogle()方法将把您重定向到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);
}
public异步任务ExternalLoginCallback(string returnUrl=null,string remoteError=null)
{
var info=await HttpContext.AuthenticateAsync(“ExternalCookie”);
//登录本地cookie并注销外部cookie
等待HttpContext.SignInAsync(“MainCookie”,info.Principal);
等待HttpContext.SignOutAsync(“ExternalCookie”);
//此时将删除ExternalCookie。
返回重定向到本地(returnUrl);
}
如果您现在想对任何方法进行身份验证,可以按如下所示进行:
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;
}
};
});
[Authorize(AuthenticationSchemes = "MainCookie")]
public async Task<IActionResult> Contact()
{
//Only authenticated users are allowed.
}
[授权(AuthenticationSchemes=“MainCookie”)]
公共异步任务联系人()
{
//只允许经过身份验证的用户。
}
在注销操作中添加此代码
返回重定向(“”);
可以在此处找到“RedirectToLocal”方法:创建.AspNetCore.MainCookie,但应用程序不再经过“身份验证”,User.Identity.IsAuthenticated始终为false