为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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 关注
  • 56 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信