10 blazor
我正在尝试制作 AuthenticationStateProvider 的新版本,以便我可以使用数据库中已有的数据。我称之为 ServerAuthenticationStateProvider:
using Microsoft.AspNetCore.Components;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace BadgerWatchWeb.Services
{
public class ServerAuthenticationStateProvider : AuthenticationStateProvider
{
string UserId;
string Password;
bool IsAuthenticated = false;
public void LoadUser(string _UserId, string _Password)
{
UserId = _UserId;
Password = _Password;
}
public async Task LoadUserData()
{
var securityService = new SharedServiceLogic.Security();
try
{
var passwordCheck = await securityService.ValidatePassword(UserId, Password);
IsAuthenticated = passwordCheck == true ? true : false;
} catch(Exception ex)
{
Console.WriteLine(ex);
}
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var userService = new UserService();
var identity = IsAuthenticated
? new ClaimsIdentity(await userService.GetClaims(UserId))
: new ClaimsIdentity();
var result = new AuthenticationState(new ClaimsPrincipal(identity));
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
}
我的配置服务看起来像:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<UserService>();
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
services.AddAuthorizationCore();
}
Run Code Online (Sandbox Code Playgroud)
然后我注入@inject ServerAuthenticationStateProvider AuthenticationStateProvider到我的剃刀视图文件中。
当我运行代码时,我得到 InvalidOperationException: Cannot provide a value for property 'AuthenticationStateProvider' on type 'BadgerWatchWeb.Pages.Index'. There is no registered service of type 'BadgerWatchWeb.Services.ServerAuthenticationStateProvider'.
你应该更换
@inject ServerAuthenticationStateProvider AuthenticationStateProvider
Run Code Online (Sandbox Code Playgroud)
和
@inject AuthenticationStateProvider AuthenticationStateProvider
Run Code Online (Sandbox Code Playgroud)
因为那是你注册的方式。
注意:向DI容器添加服务时,左侧参数应为接口(或抽象类),右侧参数应为具体类,如下所示:
services.AddScoped<IJSRuntime, RemoteJSRuntime>();
Run Code Online (Sandbox Code Playgroud)
您可以像这样将 IJSRuntime 类型注入到您的组件中:
@inject IJSRuntime JSRuntime
Run Code Online (Sandbox Code Playgroud)
IJSRuntime 是要注入的类型,JSRuntime 是将包含注入实例的属性的名称
为什么将您的类称为ServerAuthenticationStateProvider 这与 Blazor 框架添加到您的 DI 容器中的 AuthenticationStateProvider 的名称完全相同:
ComponentServiceCollectionExtensions.AddServerSideBlazor 方法:
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
Run Code Online (Sandbox Code Playgroud)
请根据上面的评论修改您的应用程序,运行您的应用程序,并报告错误,如果有的话...
重要的
确保您了解 AuthenticationStateProvider 类型的角色和功能、如何使用身份验证状态数据填充、何时以及由谁填充。您应该从 Blazor 定义的 ServerAuthenticationStateProvider 类型派生您的自定义 AuthenticationStateProvider ,因为此类型具有由 CircuitHost 调用的方法来设置身份验证状态...
我没有仔细阅读您的 AuthenticationStateProvider 自定义实现中的代码,但我会说它必须包含仅与身份验证状态有关的逻辑,而没有其他内容(关注点分离)。而且,是的,仅在必要时使用它。
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
9361 次 |
| 最近记录: |