我正在学习Blazor,需要一些帮助.
我的Blazor服务器项目需要扩展的身份用户.
我想访问登录显示.文件中的FirstName属性,如下所示.
我发现的大多数讨论都太陈旧了,或者没有解决我的具体问题.
我的项目用户
using Microsoft.AspNetCore.Identity;
namespace MyProject.Data;
public class 我的项目用户 : IdentityUser
{
[PersonalData]
public string? FirstName { get; set; }
[PersonalData]
public string? LastName { get; set; }
}
我的项目上下文
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace MyProject.Data;
public class 我的项目上下文 : IdentityDbContext<我的项目用户>
{
public 我的项目上下文(DbContextOptions<我的项目上下文> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
MyProject的Program.cs
using MyProject.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Components.Authorization;
using MyProject.Areas.Identity;
namespace MyProject
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("我的项目上下文") ?? throw new InvalidOperationException("Connection string '我的项目上下文' not found.");
builder.Services.AddDbContext<我的项目上下文>(options => options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<我的项目用户>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<我的项目上下文>();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<我的项目用户>>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
}
}
}
一切都很顺利,数据库也更新了等等.
I simply want to access the 我的项目用户's FirstName property here:
登录显示.
<CascadingAuthenticationState>
<AuthorizeView>
<Authorized>
<a href="Identity/Account/Manage">Hello, -->@context.User.Identity?.Name!</a>
<form method="post" action="Identity/Account/Logout">
<button type="submit" class="nav-link btn btn-link">Log out</button>
</form>
</Authorized>
<NotAuthorized>
<a href="Identity/Account/Register">Register</a>
<a href="Identity/Account/Login">Log in</a>
</NotAuthorized>
</AuthorizeView>
</CascadingAuthenticationState>
LoginDisplay在MainLayout.razor中使用
@using Microsoft.AspNetCore.Identity;
@using MyProject.Data;
@inherits LayoutComponentBase
@inject UserManager<我的项目用户> UserManager;
@inject AuthenticationStateProvider GetAuthenticationStateAsync
<PageTitle>MyProject</PageTitle>
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<LoginDisplay />
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<article class="content px-4">
@Body
</article>
</main>
</div>
如有任何帮助,我们不胜感激.