我正在尝试在 blazor 中制作客户授权代码。根据之前的 stackoverflow 帖子,我构建了以下内容。我的问题是标签仍未注册。我有:
public class MyServerAuthenticationStateProvider : AuthenticationStateProvider
{
string UserId;
string Password;
bool IsAuthenticated = false;
private ClaimsPrincipal claimsPrincipal;
public void LoadUser(string _UserId, string _Password)
{
UserId = _UserId;
Password = _Password;
}
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;
}
}
和:
@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; }
protected override async Task OnInitializedAsync()
{
var myStateProvider = AuthenticationStateProvider as MyServerAuthenticationStateProvider;
myStateProvider.LoadUser("mperry", "testtest");
await myStateProvider.GetAuthenticationStateAsync();
}
}
我真的需要知道这是否是在 blazoe 中进行自定义身份验证的公认方式。