【发布时间】:2021-09-13 16:55:58
【问题描述】:
我正在制作自定义 AuthenticationStateProvider 以在 Blazor 应用程序中进行测试。我担心新类不会具有与 AuthenticationStateProvider 类相同的功能,因为我不确定 AuthenticationStateProvider 是如何工作的。下面我发布了我的自定义类。你能告诉我这是否是覆盖这个类的公认方式吗?
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;
}
}
【问题讨论】:
-
别担心...你看到我对你上一个问题的回答了吗?在我看来,你从中得到的唯一东西就是担心......