C# 修改标识或添加声明
C# Modifying Identity or Add a Claim
应用程序是 Blazor Server .NET Core 5.0
我正在使用 .NET Core 的身份系统,但我 运行 遇到了问题。我想将此人的名字与身份一起存储,以便我可以轻松调用它。
目前我有一个 class 覆盖基本身份:
public class ApplicationUser : IdentityUser
{
public ApplicationUser() : base() { }
[StringLength(100)]
public string FirstName { get; set; }
[StringLength(100)]
public string LastName { get; set; }
}
我的 startup.cs 有:
services.AddDefaultIdentity<ApplicationUser>(options => {
options.SignIn.RequireConfirmedAccount = true;
}
)
在我看来,在寻找身份时,应该强制程序的智能感知理解 ApplicationUser 是默认值 class。
但是,当我尝试调用时:
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
它只有 returns“IdentityUser”数据,而不是我自定义的 ApplicationUser class。
我是否缺少 AuthenticationStateProvider 返回的内容或我缺少的类型转换?
此外,如果这完全不正确,我应该对 Claims 执行此操作吗?如果是这样,我无法找到有效使用 Blazor Server 声明的具体方法。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
在程序包管理器控制台中执行:
- 添加迁移 CreateAppUser
- 更新数据库
将值插入在用户中创建的 FirstName 和 LastName table
创建一个 class 名为:ApplicationUserClaimsTransformation
ApplicationUserClaimsTransformation.cs
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);
}
}
Startup.ConfigureServices
services.AddScoped<IClaimsTransformation,
ApplicationUserClaimsTransformation>();
Index.razor
@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();
}
}
注意:您应该将 IdentityUser 名称更改为 ApplicationUser
启动 class、LogOut.cshtml 文件、_LoginPartial.cshtml 文件和
在 ApplicationDbContext 的定义中 class:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
应用程序是 Blazor Server .NET Core 5.0
我正在使用 .NET Core 的身份系统,但我 运行 遇到了问题。我想将此人的名字与身份一起存储,以便我可以轻松调用它。
目前我有一个 class 覆盖基本身份:
public class ApplicationUser : IdentityUser
{
public ApplicationUser() : base() { }
[StringLength(100)]
public string FirstName { get; set; }
[StringLength(100)]
public string LastName { get; set; }
}
我的 startup.cs 有:
services.AddDefaultIdentity<ApplicationUser>(options => {
options.SignIn.RequireConfirmedAccount = true;
}
)
在我看来,在寻找身份时,应该强制程序的智能感知理解 ApplicationUser 是默认值 class。
但是,当我尝试调用时:
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
它只有 returns“IdentityUser”数据,而不是我自定义的 ApplicationUser class。
我是否缺少 AuthenticationStateProvider 返回的内容或我缺少的类型转换?
此外,如果这完全不正确,我应该对 Claims 执行此操作吗?如果是这样,我无法找到有效使用 Blazor Server 声明的具体方法。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
在程序包管理器控制台中执行:
- 添加迁移 CreateAppUser
- 更新数据库
将值插入在用户中创建的 FirstName 和 LastName table
创建一个 class 名为:ApplicationUserClaimsTransformation
ApplicationUserClaimsTransformation.cs
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);
}
}
Startup.ConfigureServices
services.AddScoped<IClaimsTransformation,
ApplicationUserClaimsTransformation>();
Index.razor
@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();
}
}
注意:您应该将 IdentityUser 名称更改为 ApplicationUser 启动 class、LogOut.cshtml 文件、_LoginPartial.cshtml 文件和 在 ApplicationDbContext 的定义中 class:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>