我正在尝试使用自定义数据库来实现登录。据我所知,我需要重写AuthenticationStateProvider来完成此操作。
在MyServerAuthenticationStateProvider.cs中:
public class MyServerAuthenticationStateProvider : 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中:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BadgerWatchWeb.Services;
namespace BadgerWatchWeb
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<UserService>();
services.AddAuthorizationCore();
services.AddScoped<AuthenticationStateProvider, MyServerAuthenticationStateProvider > ();
//services.AddScoped<AuthenticationStateProvider>(provider => provider.GetRequiredService<MysServerAuthenticationStateProvider>());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub<App>(selector: "app");
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
当我尝试在.razor类中使用此服务时,收到一条错误消息,提示“ MyServerAuthenticationStateProvider不包含LoadUser的定义。”
@page "/"
@using BadgerWatchWeb.Services
@inject AuthenticationStateProvider AuthenticationStateProvider
<h1>Sup</h1>
<AuthorizeView>
<Authorized>
<h1>Hello, @context.User.Identity.Name!</h1>
<p>You can only see this content if you're authenticated.</p>
</Authorized>
<NotAuthorized>
<h1>Authentication Failure!</h1>
<p>You're not signed in.</p>
</NotAuthorized>
<Authorizing>
<h1>Authorizing</h1>
</Authorizing>
</AuthorizeView>
@code {
[CascadingParameter] Task<AuthenticationState> authenticationStateTask { get; set; }
AuthenticationState AuthState;
protected override async Task OnInitializedAsync()
{
AuthenticationStateProvider.LoadUser("mperry", "testtest");
AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
}
}
我不确定是否不确定我是否正确使用了AuthenticationStateProvider,也无法在线找到有关如何在razor中实现自定义登录的示例。但是我的问题是:即使MyServerAuthenticationProvider在Startus.cs中被定义为AuthenticationStateProvider的范围,为什么我的代码也无法识别LoadUser。
答案 0 :(得分:1)
在DI上,您注入了自定义提供程序是正确的事情:
services.AddScoped<AuthenticationStateProvider,
MyServerAuthenticationStateProvider > ();
要访问您的自定义提供程序,只需进行强制转换:
@inject AuthenticationStateProvider AuthenticationStateProvider
@code {
protected override async Task OnInitializedAsync()
{
var myStateProv = AuthenticationStateProvider as MyServerAuthenticationStateProvider;
myStateProv.LoadUser("mperry", "testtest");