Blazor 服务器:针对 Identity Server 4 进行身份验证并使用 cookie 进行本地身份验证
Blazor Server: authenticating against Identity Server 4 and using cookies for local authentication
我正在尝试构建一个服务器端 Blazor 应用程序,它允许用户登录 Identity Server 4 并使用 Cookie 来处理本地授权。这是我当前设置的代码:
services.AddAuthentication(options => {
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc",
options => {
Configuration.GetSection("OidcSettings").Bind(options);
// remove unecessary claims
options.ClaimActions.MapAllExcept("amr", "sub");
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = ClaimTypes.NameIdentifier
};
});
我想使用 nameidentifier 声明作为用户名,我的印象是之前的映射应该可以解决这个问题。不幸的是,它不起作用。这是一些演示代码:
@page "/"
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor ContextAccessor
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
<h1>Hello, world!</h1>
Welcome to your new app.
<AuthorizeView>
<NotAuthorized>
<form method="post" action="/Account/Login">
<button>Iniciar</button>
</form>
</NotAuthorized>
<Authorized>
<h1>Hi, @ContextAccessor.HttpContext.User.Identity.Name!</h1>
<h1>Hi, @(GetUsernameAsync().Result)!</h1>
</Authorized>
</AuthorizeView>
@code{
private async Task<string> GetUsernameAsync() {
var user1 = ContextAccessor.HttpContext.User.Identity.Name;
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
return user.Identity.Name;
}
return "Nop";
}
}
在这两种情况下,用户名始终设置为空。这是我从调试器得到的:
有几件事我没有得到:
- 为什么我得到这种身份验证类型?它不应该是 cookie 或应用程序 cookie(或类似的东西)吗?
- 为什么名称声明没有更改为名称标识符?
我肯定遗漏了什么,但我不确定是什么...
谢谢
好的,经过多次测试,我终于成功了。我已经开始使用 sub 声明作为我的用户名。我不得不更改 OpenId Connect 设置中的一些内容:
.AddOpenIdConnect("oidc",
options => {
Configuration.GetSection("OidcSettings").Bind(options);
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = "sub"
};
});
我正在尝试构建一个服务器端 Blazor 应用程序,它允许用户登录 Identity Server 4 并使用 Cookie 来处理本地授权。这是我当前设置的代码:
services.AddAuthentication(options => {
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc",
options => {
Configuration.GetSection("OidcSettings").Bind(options);
// remove unecessary claims
options.ClaimActions.MapAllExcept("amr", "sub");
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = ClaimTypes.NameIdentifier
};
});
我想使用 nameidentifier 声明作为用户名,我的印象是之前的映射应该可以解决这个问题。不幸的是,它不起作用。这是一些演示代码:
@page "/"
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor ContextAccessor
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
<h1>Hello, world!</h1>
Welcome to your new app.
<AuthorizeView>
<NotAuthorized>
<form method="post" action="/Account/Login">
<button>Iniciar</button>
</form>
</NotAuthorized>
<Authorized>
<h1>Hi, @ContextAccessor.HttpContext.User.Identity.Name!</h1>
<h1>Hi, @(GetUsernameAsync().Result)!</h1>
</Authorized>
</AuthorizeView>
@code{
private async Task<string> GetUsernameAsync() {
var user1 = ContextAccessor.HttpContext.User.Identity.Name;
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
return user.Identity.Name;
}
return "Nop";
}
}
在这两种情况下,用户名始终设置为空。这是我从调试器得到的:
有几件事我没有得到:
- 为什么我得到这种身份验证类型?它不应该是 cookie 或应用程序 cookie(或类似的东西)吗?
- 为什么名称声明没有更改为名称标识符?
我肯定遗漏了什么,但我不确定是什么...
谢谢
好的,经过多次测试,我终于成功了。我已经开始使用 sub 声明作为我的用户名。我不得不更改 OpenId Connect 设置中的一些内容:
.AddOpenIdConnect("oidc",
options => {
Configuration.GetSection("OidcSettings").Bind(options);
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = "sub"
};
});