如何使用组织身份验证从 Blazor Server 中的身份获取用户属性
How to get user's attributes from Identity in Blazor Server with organization authentication
在为身份验证提供的模板中,您会看到 LoginDisplay 组件中显示了一个电子邮件地址,这就是 ...Identity
的 Name
属性,如下所示:
<AuthorizeView>
<Authorized>
Hello, @context.User.Identity.Name!
<a href="AzureAD/Account/SignOut">Log out</a>
</Authorized>
<NotAuthorized>
<a href="AzureAD/Account/SignIn">Log in</a>
</NotAuthorized>
</AuthorizeView>
我想知道的是如何访问有关经过身份验证的用户的其他信息。例如全名,以一种简单的方式。我发现使用 Linq
您可以像这样从 Claims
属性 获取数据:
@context.User.Claims.Where(c => c.Type == "name").FirstOrDefault().Value.ToString()
这是我们获取该信息的方式,还是有 better/preferred 方式从经过身份验证的用户访问该数据?
如果您有 Claims
中需要的东西,这是获取它的最佳地点。您可以向您的 Identity
添加额外的声明 - 在 Adding extra claims in ASP.NET Core web applications. If it's not enough and you'd like to get full access to your IdentityUser
entity inside your authorized component markup, you can do that via AuthenticationStateProvider
和 UserManager<IdentityUser>
中查看如何通过将它们注入您的 Blazor
页面来做到这一点。
@page "/"
@inject AuthenticationStateProvider AuthenticationStateProvider
@using Microsoft.AspNetCore.Identity;
@inject UserManager<IdentityUser> UserManager;
<p>@Details</p>
@code {
private string Details { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var user = await _UserManager.FindByNameAsync(user.Identity.Name)
Details = $"Your user phone # is {user.PhoneNumber}.";
}
else
{
Details = "The user is NOT authenticated.";
}
}
}
在为身份验证提供的模板中,您会看到 LoginDisplay 组件中显示了一个电子邮件地址,这就是 ...Identity
的 Name
属性,如下所示:
<AuthorizeView>
<Authorized>
Hello, @context.User.Identity.Name!
<a href="AzureAD/Account/SignOut">Log out</a>
</Authorized>
<NotAuthorized>
<a href="AzureAD/Account/SignIn">Log in</a>
</NotAuthorized>
</AuthorizeView>
我想知道的是如何访问有关经过身份验证的用户的其他信息。例如全名,以一种简单的方式。我发现使用 Linq
您可以像这样从 Claims
属性 获取数据:
@context.User.Claims.Where(c => c.Type == "name").FirstOrDefault().Value.ToString()
这是我们获取该信息的方式,还是有 better/preferred 方式从经过身份验证的用户访问该数据?
如果您有 Claims
中需要的东西,这是获取它的最佳地点。您可以向您的 Identity
添加额外的声明 - 在 Adding extra claims in ASP.NET Core web applications. If it's not enough and you'd like to get full access to your IdentityUser
entity inside your authorized component markup, you can do that via AuthenticationStateProvider
和 UserManager<IdentityUser>
中查看如何通过将它们注入您的 Blazor
页面来做到这一点。
@page "/"
@inject AuthenticationStateProvider AuthenticationStateProvider
@using Microsoft.AspNetCore.Identity;
@inject UserManager<IdentityUser> UserManager;
<p>@Details</p>
@code {
private string Details { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var user = await _UserManager.FindByNameAsync(user.Identity.Name)
Details = $"Your user phone # is {user.PhoneNumber}.";
}
else
{
Details = "The user is NOT authenticated.";
}
}
}