在布拉佐爾中:我正在創建一個自定義 AuthenticationStateProvider 類,以便我可以使用托管在 mssql 上的自定義用戶數據庫。我的自定義類是:public class ServerAuthenticationStateProvider : AuthenticationStateProvider{ string UserId; string Password; public void LoadUser(string _UserId, string _Password) { UserId = _UserId; Password = _Password; } public override async Task<AuthenticationState> GetAuthenticationStateAsync() { var securityService = new SharedServiceLogic.Security(); var userService = new UserService(); var validPassword = await securityService.ValidatePassword(UserId, Password); var authenticated = validPassword == true ? true : false; var identity = authenticated ? new ClaimsIdentity(await userService.GetClaims(UserId), "AuthCheck") : new ClaimsIdentity(); var result = new AuthenticationState(new ClaimsPrincipal(identity)); return result; }}然后我在 Startup.cs 中注冊它: public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddServerSideBlazor(); services.AddSingleton<UserService>(); services.AddAuthorizationCore(); services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>(); }我的 App.razor 是:<CascadingAuthenticationState> <Router AppAssembly="typeof(Startup).Assembly"> <NotFoundContent> <p>Sorry, there's nothing at this address.</p> </NotFoundContent> </Router></CascadingAuthenticationState>現在我想使用 Index.razor 中的服務:@page "/"@using BadgerWatchWeb.Services@inject AuthenticationStateProvider AuthenticationStateProvider<h1>Sup</h1>由于錯誤,我無法運行此代碼。錯誤說AuthenticationStateProvider does not contain a definition of LoadUser。我認為該服務將能夠使用 ServerAuthenticationStateProvider 中的類。難道不是這樣嗎?
1 回答

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
ServerAuthenticationStateProvider是 Blazor 在服務器端 Blazor配置中添加的默認 AuthenticationStateProvider 的名稱,實際上,它不包含 LoadUser 的定義
這就是 Steve Anderson 關于自定義 AuthenticationStateProvider 的說法
對于服務器端 Blazor,您不太可能實現自定義 AuthenticationStateProvider。內置實現已經與 ASP.NET Core 的內置身份驗證機制集成。如果您實施自定義的,則可能會引入安全漏洞。
我建議您仔細閱讀 ServerAuthenticationStateProvider 類,了解它的作用和用途,遵循整個過程,然后決定是否創建自定義的 AuthenticationStateProvider
希望這可以幫助
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報
0/150
提交
取消