c# - ASP.NET Core 3.1 上的 Google Auth2.0 登录错误
问题描述
我有一个用于登录的 google auth2.0。我尝试了很多方法来解决,但没有任何改变。我认为这与 nginx 和 .NET Core 设置有关。我没有使用身份。但我还没有找到任何东西。我的代码如下所示。
注意:此错误仅发生在我们的移动应用程序(Android webview)中;Web 应用程序工作正常。
Startup.cs
(ConfigureServices
方法)
...
services.AddAuthentication(auth =>
{
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
AddGoogle(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
IConfigurationSection googleAuthNSection = configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.SaveTokens = true;
options.CorrelationCookie.SameSite = SameSiteMode.None;
options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
})
.AddCookie(options =>
{
options.LoginPath = "/Account";
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.IsEssential = true;
});
...
Startup.cs
:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCustomExceptionMiddleware();
app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
app.UseRouting();
app.UseCookiePolicy();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
AccountController.cs
(当点击谷歌登录按钮时,它会调用GoogleLogin
方法)
[HttpGet]
public IActionResult GoogleLogin()
{
var properties = new AuthenticationProperties
{
RedirectUri = Url.Action("GoogleResponse")
};
return Challenge(properties, GoogleDefaults.AuthenticationScheme);
}
[HttpGet]
public async Task<IActionResult> GoogleResponse()
{
_logger.LogInformation("Google auth started");
// some codes
_logger.LogInformation("Google auth finished");
return RedirectToAction("Index", "Home");
}
这是我的错误堆栈跟踪。我在日志中使用了 '~~' 字符
警告:Microsoft.AspNetCore.Authentication.Google.GoogleHandler[15]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5,
Parent .AspNetCore.Correlation.Google.wewjzQ0oDhkb63dSD .....' cookie 未找到。
info: Microsoft.AspNetCore.Authentication.Google.GoogleHandler[4]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5, ParentId:
错误从 RemoteAuthentication: Correlation failed..
警告: WebUI.Middleware.CustomExceptionMiddleware[0]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5, ParentId:
未处理异常 URL 的 URL:https ://example.com /signin-google??state=XXX&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo。 email&authuser=0&prompt=none 失败:WebUI.Middleware.CustomExceptionMiddleware[0]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46d5d549d
~~ 未处理的异常消息:处理远程登录时遇到错误。
失败:WebUI.Middleware.CustomExceptionMiddleware[0]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5, ParentId:
~~ 未处理的异常:
在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke
(HttpContext context)
在 Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
在 Microsoft.AspNetCore.Session 的 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()。
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext 上下文)上的SessionMiddleware.Invoke(HttpContext上下文)
在 /home/MY_PROJECT_PATH/Middleware/CustomExceptionMiddleware.cs:line 50 中的 WebUI.Middleware.CustomExceptionMiddleware.Invoke(HttpContext httpContext)失败:WebUI.Middleware.CustomExceptionMiddleware[0]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5, ParentId:
~~未处理的消息: 关联失败。失败:WebUI.Middleware.CustomExceptionMiddleware[0]
=> ConnectionId:0HM50OGGO9P17 => RequestPath:/signin-google RequestId:0HM50OGGO9P17:00000001, SpanId:|6234ca08-46b49a3bf35855d5., TraceId:6234ca08-46b49a3bf35855d5, ParentId:
~~ 未处理的异常: (无效的)
编辑:
我注意到了一些东西。移动应用用户首先登录(我将用户的登录页面信息写入数据库),但第二个页面需要表单发布。当人们提交此表单(需要上传 PDF)时,会出现上述错误。这个错误给了移动用户,但是很少有移动用户正常进行
解决方案
推荐阅读
- e2e-testing - 发生 500 xhr 请求时,cypress 测试失败
- javascript - 如何在 react-bootstrap 选择选项中使用翻译
- module - 模块的 NestJs 版本
- api - 为什么我无法显示来自 YouTube 直播的嵌入视频?
- angular - 删除本地文件 Ionic 4
- uml - 如何为无需登录的用户创建一个类?
- c# - 如何在 WebApi 中将 Get 请求转换为 Post 请求?
- java - 在 Intellij 2019.1 中使用 Open JDK 11.0.2 构建 SpringBoot 项目失败
- scala - 在惰性流中保留共享意味着什么?
- python - Send whatsapp message to contacts using Python but getting an error: InvalidSelectorException: Message: invalid selector: Unable to locate an element