gpt4 book ai didi

c# - 谷歌身份验证 ASP.NET Core Web Api

转载 作者:太空狗 更新时间:2023-10-30 01:29:39 29 4
gpt4 key购买 nike

当 google 重定向到它抛出的回调方法时我遇到了一些问题 Exception: The oauth state was missing or invalid.

启动.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Conte>(config =>
config.UseSqlServer(Configuration.GetConnectionString("Identity")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<Conte>()
.AddDefaultTokenProviders();

services.AddAuthentication()
.AddCookie("Cook")
.AddGoogle(config =>
{
config.SignInScheme = "Cook";
config.ClientId = Configuration["Authentication:Google:Client_Id"];
config.ClientSecret = Configuration["Authentication:Google:Client_Secret"];

config.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "UserId");
config.ClaimActions.MapJsonKey(ClaimTypes.Email, "EmailAddress", ClaimValueTypes.Email);
config.ClaimActions.MapJsonKey(ClaimTypes.Name, "Name");

});

services.AddMvc();
}

AccountController.cs

[AllowAnonymous]
[HttpGet]
[Route("/api/google-login")]
public async Task LoginGoogle()
{
await HttpContext.ChallengeAsync("Google", new AuthenticationProperties() { RedirectUri = "/signin-google" });
}

[AllowAnonymous]
[HttpGet]
[Route("/signin-google")]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
var info = await _signInManager.GetExternalLoginInfoAsync();

// Sign in the user with this external login provider if the user already has a login.
var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (result.Succeeded)
{
return Redirect(returnUrl);
}
return BadRequest();
}

它去 Google Account

当我绑定(bind)授权时抛出异常

最佳答案

根据tutorial来自微软:

The Google authentication configured later in this tutorial will automatically handle requests at /signin-google route to implement the OAuth flow.

/signin-google 路由由中间件处理,而不是由您的 MVC Controller 处理。您的外部登录应该路由到类似/ExternalLoginCallback

关于c# - 谷歌身份验证 ASP.NET Core Web Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762800/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com