我在让 SSO 工作时遇到了一个问题,这很令人困惑。下面配置中的 CallBackPath 与 Azure 中的重定向 uri 相匹配,我可以在日志中看到用户已成功通过身份验证。然后发生的事情是,他们在开发工具中的网络选项卡中收到 302,然后在页面上变成 502。
我不确定我是否遗漏了什么,或者配置有误。有人能提供建议吗?
我的配置:
"Authentication": {
"ClientId": "xxxxxx",
"ClientSecret": "yyyyyyyy",
"Authority": "https://login.microsoftonline.com/zzzzzzz",
"CallbackPath": "/mypath/mypage",
"Scope": "openid profile email"
},
我想要启动 SSO 的登录页面的路由:
[AllowAnonymous]
[Route("login")]
public IActionResult Login()
{
return Challenge(new AuthenticationProperties(), OpenIdConnectDefaults.AuthenticationScheme);
}
以及 program.cs 中的相关部分
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("OpenIdConnect", options =>
{
options.ClientId = builder.Configuration["Authentication:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:ClientSecret"];
options.Authority = builder.Configuration["Authentication:Authority"];
options.CallbackPath = builder.Configuration["Authentication:CallbackPath"];
options.ResponseType = "code";
options.SkipUnrecognizedRequests = true;
options.SaveTokens = true;
options.Events = new OpenIdConnectEvents
{
OnTokenValidated = async context =>
{
// Log successful authentication
var userId = context.Principal?.Claims.FirstOrDefault(c => c.Type == "name")?.Value
?? context.Principal?.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value
?? "Unknown User";
Log.Information("SSO Log - User {UserId} successfully authenticated via SSO.", userId);
await Task.CompletedTask;
},
OnAuthenticationFailed = context =>
{
Log.Error("SSO Log - Authentication failed: {Error}", context.Exception.Message);
return Task.CompletedTask;
}
};
});