未使用自定义授权服务,即使它已在服务中声明

在布拉佐尔中:


我正在创建一个自定义 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 中的类。难道不是这样吗?


函数式编程
浏览 59回答 1
1回答

开心每一天1111

ServerAuthenticationStateProvider是 Blazor 在服务器端 Blazor配置中添加的默认 AuthenticationStateProvider 的名称,实际上,它不包含 LoadUser 的定义这就是 Steve Anderson 关于自定义 AuthenticationStateProvider 的说法对于服务器端 Blazor,您不太可能实现自定义 AuthenticationStateProvider。内置实现已经与 ASP.NET Core 的内置身份验证机制集成。如果您实施自定义的,则可能会引入安全漏洞。我建议您仔细阅读 ServerAuthenticationStateProvider 类,了解它的作用和用途,遵循整个过程,然后决定是否创建自定义的 AuthenticationStateProvider希望这可以帮助
打开App,查看更多内容
随时随地看视频慕课网APP