我有一個使用 Windows 身份驗證來識別用戶的 Blazor Server 應用程式。該應用程式“檢測”當前瀏覽該應用程式的用戶,并可以正確顯示域/用戶名。
我撰寫了一個類,它檢查登錄的用戶名并從資料庫中獲取角色,然后更新當前主體,以便 UI 可以正確利用用戶的給定角色。
由于無法在此處分享的原因,我正在避免使用 EntityFramework 和核心標識腳手架代碼。
這是我寫的課
public class CompanyAuthentication
{
public CompanyAuthentication(AuthenticationStateProvider auth)
{
var result = auth.GetAuthenticationStateAsync().Result;
// here I have access to the current username:
string username = result.User.Identity.Name;
// now I can fetch roles from file, but for simplicity, I will use the following:
if(username.BeginsWith("admin"))
{
var claims = new ClaimsIdentity(new Claim[] {new Claim(ClaimType.Role, "admin")});
result.User.AddIdentity(claims); // this will have an effect on UI
}
}
}
我還在 Startup.cs 中添加了上述類作為服務
services.AddScoped<CompanyAuthentication>();
現在,在任何剃須刀頁面中,我只需執行以下操作:
@page "/counter"
@inject CompanyAuthentication auth
<AuthorizeView Roles="admin">
<Authorized> Welcome admin </Authorized>
<NotAuthorized> You are not authorized </NotAuthorized>
</AuthorizeView>
這一切都很好,只要我不忘記CompanyAuthentication在我打算使用的每個頁面中注入。
有沒有辦法自動將我的課程注入每個頁面而無需這樣做@inject?我知道我可以撰寫一個繼承的自定義身份驗證類,AuthenticationStateProvider然后將其添加為服務,但如果這樣做,我將無法訪問當前登錄的用戶名,因此我無法從資料庫中獲取角色。
我嘗試使用HttpContext.Current.User.Identity.Name,但這不在 Balzor 服務器應用程式的任何部分的范圍內。
如何利用 Windows AuthenticationStateProvider 并同時自定義其角色,而不必到處注入自定義?
uj5u.com熱心網友回復:
您應該能夠撰寫自定義AuthenticationStateProvider. 我已經設定了一個 Blazor Server 站點,并將身份驗證設定為 Windows。
自定義 AuthenticationStateProvider。請注意,它繼承自ServerAuthenticationStateProvider.
public class MyAuthenticationStateProvider : ServerAuthenticationStateProvider
{
private bool _isNew = true;
public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{
var state = await base.GetAuthenticationStateAsync();
// Add your code here to get the user info to use in the logic of what roles you add
// only add the extra Identity once
if (_isNew)
state.User.AddIdentity(AdminIdentity);
_isNew = false;
return state;
}
private ClaimsIdentity AdminIdentity
=> new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, "admin") }, "My Auth Type");
}
在服務中注冊它 - 注意順序:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// Add custom AuthenticationStateProvider after AddServerSideBlazor will overload the existing registered service
builder.Services.AddScoped<AuthenticationStateProvider, MyAuthenticationStateProvider>();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
這是一個向您展示宣告的測驗頁面:
@page "/"
@inject AuthenticationStateProvider Auth;
@using System.Security.Claims
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
@if(user is not null)
{
@foreach(var claim in user.Claims)
{
<div>
@claim.Type : @claim.Value
</div>
}
}
@code{
private ClaimsPrincipal user = default!;
protected async override Task OnInitializedAsync()
{
var state = await Auth.GetAuthenticationStateAsync();
user = state.User;
}
}
這是一個螢屏截圖,顯示了用戶 Windows 安全資訊和添加的角色。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/497080.html
