在布拉佐尔中:
我正在创建一个自定义 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 中的类。难道不是这样吗?
开心每一天1111
相关分类