使用OpenID Connect注销后强制登录,并在Blazor(.Net 6)上设置寿命cookie



我在.Net 6服务器上使用Blazor,在使用OpenID Connect到ADFS进行身份验证后,我在注销和cookie方面遇到了一些问题。

第一次登录运行良好(我看到了adfs页面(。问题是当用户注销时,使用自定义注销页面,如果用户试图访问受保护的页面(注销后(,则可以在不登录的情况下再次访问。几秒钟后,在地址栏中,它会显示ADFS的呼叫,然后进行身份验证,但不显示任何用户名和密码表单。如何在每次注销后强制用户登录ADFS?

对于此配置,我遵循以下示例:Blazor OpenID Connect

另一件事是Cookie的时间跨度。我在.AddCookie((方法中尝试了一些解决方案,但没有成功。如何正确设置Cookie时间跨度(10分钟(?

最后一个问题是/.openid配置。在从Duende Identity Server更改为此简单解决方案(下面的代码(之前,我无法到达此端点。为什么?

Setup.cs文件:

public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
opt.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://adfs......./adfs";
options.ClientId = "<my_ID>";
options.ClientSecret = "<my_Secret>";
options.SignInScheme = "Cookies";
options.RequireHttpsMetadata = false;
options.ResponseType = "id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.UseTokenLifetime = false;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" };
options.Events = new OpenIdConnectEvents
{
OnAccessDenied = context =>
{
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthentication();
app.UseAuthorization();
}

注销.cshtml

@page
@model Service.Pages.LogoutModel
@{
}

注销.cs.html.cs

namespace Service.Pages
{
public class LogoutModel : PageModel
{
public async Task<IActionResult> OnGetAsync()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
return Redirect("/");
}
}
}

我解决了关于/.众所周知的端点的问题。我弄错了服务器名,现在可以正常工作了。当您使用Duende Identity Server时,此配置页面与您的项目相同(例如。https://localhost:5001/.well-know/openid配置(,但如果没有它,路径是:

https://[您的adfs服务器]/adfs/众所周知的/openid配置

选项中配置相同。Authority.

现在还不确定为什么注销不能正常工作。。也许这是民主同盟军的问题。

最新更新