cob*_*208 3 c# claims-based-identity asp.net-identity blazor
应用程序是 Blazor Server .NET Core 5.0
我正在使用 .NET Core 的身份系统,但遇到了问题。我希望将这个人的名字与身份一起存储,以便我可以轻松地调用它。
目前我有一个类覆盖基本身份:
public class ApplicationUser : IdentityUser
{
public ApplicationUser() : base() { }
[StringLength(100)]
public string FirstName { get; set; }
[StringLength(100)]
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的startup.cs有:
services.AddDefaultIdentity<ApplicationUser>(options => {
options.SignIn.RequireConfirmedAccount = true;
}
)
Run Code Online (Sandbox Code Playgroud)
在我看来,这应该迫使程序的智能感知在寻找身份时理解 ApplicationUser 是默认类。
但是,当我尝试打电话时:
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
Run Code Online (Sandbox Code Playgroud)
它只返回“IdentityUser”数据,而不是我的自定义 ApplicationUser 类。
我是否缺少 AuthenticationStateProvider 返回的内容或缺少类型转换?
另外,如果这是完全错误的,我应该用索赔来做到这一点吗?如果是这样,我无法找到有效使用 Blazor Server 声明的具体方法。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在包管理器控制台中执行:
将值插入到在 Users 表中创建的 FirstName 和 LastName
创建一个名为:ApplicationUserClaimsTransformation 的类
using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication3.Data;
using WebApplication3.Models;
using System.Security.Claims;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Components.Authorization;
public class ApplicationUserClaimsTransformation : IClaimsTransformation
{
private readonly UserManager<ApplicationUser> _userManager;
public ApplicationUserClaimsTransformation(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var identity = principal.Identities.FirstOrDefault(c => c.IsAuthenticated);
if (identity == null) return principal;
var user = await _userManager.GetUserAsync(principal);
if (user == null) return principal;
// Add or replace identity.Claims.
if (!principal.HasClaim(c => c.Type == ClaimTypes.GivenName))
{
identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
}
if (!principal.HasClaim(c => c.Type == ClaimTypes.Surname))
{
identity.AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
}
return new ClaimsPrincipal(identity);
}
}
Run Code Online (Sandbox Code Playgroud)
services.AddScoped<IClaimsTransformation,
ApplicationUserClaimsTransformation>();
Run Code Online (Sandbox Code Playgroud)
@page "/"
@inject AuthenticationStateProvider AuthState
@using System.Security
@using System.Security.Claims
@foreach(var c in user.Claims)
{
<div>@c.Type: @c.Value</div>
}
@code
{
private ClaimsPrincipal user;
protected override async Task OnInitializedAsync()
{
var x = await AuthState.GetAuthenticationStateAsync();
user = x.User;
await base.OnInitializedAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您应该在 Startup 类、LogOut.cshtml 文件、_LoginPartial.cshtml 文件以及 ApplicationDbContext 类的定义中将 IdentityUser 名称更改为 ApplicationUser:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5216 次 |
| 最近记录: |