亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未使用自定義授權服務,即使它已在服務中聲明

未使用自定義授權服務,即使它已在服務中聲明

C#
函數式編程 2023-09-16 20:01:40
在布拉佐爾中:我正在創建一個自定義 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

希望這可以幫助


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號