【发布时间】:2022-01-22 15:08:14
【问题描述】:
目标是在使用 .NET6 的 C# MVC 应用程序中针对 Google 实施 OAuth2。我得到的结果在结果中显示失败。Succeeded 字段并且所有值都为空。
我正在发送正确的谷歌客户端 ID 和谷歌客户端密码。
应用程序构建并发送请求而没有错误。我相信这都是正确的。
这是我的 Program.cs
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();
var googleClientId = config.GetValue<string>("GoogleClientId");
var googleClientSecret = config.GetValue<string>("GoogleClientSecret");
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddGoogle(options =>
{
options.ClientId = googleClientId;
options.ClientSecret = googleClientSecret;
options.CallbackPath = "/account/google-response";
}).AddCookie(options =>
{
options.LoginPath = "/account/google-login"; // Must be lowercase
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
这是发送和接收结果的帐户控制器:
[AllowAnonymous, Route("account")]
public class AccountController : Controller
{
[Route("google-login")]
public IActionResult GoogleLogin()
{
var properties = new AuthenticationProperties { RedirectUri = Url.Action("GoogleResponse") };
return new ChallengeResult(GoogleDefaults.AuthenticationScheme, properties);
}
[Route("google-response")]
public async Task<IActionResult> GoogleResponse()
{
var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
// This is coming back with result.Succeeded == false, and result.Principal == null
var succeeded = result.Succeeded;
if (result.Principal == null) throw new Exception("Principal is null");
if (result.Principal.Identities == null) throw new Exception("Identities is null");
var claims = result.Principal.Identities
.FirstOrDefault().Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
});
return Json(claims);
}
}
不知道哪里出了问题。
感谢您的帮助。
补充:不确定这是否有帮助,但我从 Google 返回的有效负载:
状态:CfDJ8Dcp9PtNslFFr9WdoNMnaxZuUE3bX_7go4zy8_XDg2ZIar8NvdxzZlhJ9mM9c8-E3cp9TchRcjvMwbX4XaMmTC79aKO7IuI39yHgZ6nrOEqORPDU9kfHGH-bgJB5S1bXIQcJ3y3wKMD39N6IDa-ygAxqoiFkd05Lf05d9RoA8bZ0h8DcbYbqpbK73rQraTQhBAdxVJlz2CLGXkzOhIoJmhdOn38poxjNILyGPOGRqA0t 代码:4/0AX4XfWjHBoCnvig2U2JG8tuPnqIvjQeC5VN_u_pSOcqIFFOjw7UadqI04qJ3iw4QyF2ngg 范围:电子邮件配置文件 openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email 授权人:0 提示:同意
【问题讨论】:
-
蟋蟀...我想我必须做些不同的事情。 :(
标签: c# web asp.net-mvc-4 oauth-2.0 .net-6.0