using EasyTemplate.Service;
using EasyTemplate.Tool;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
namespace EasyTemplate.Blazor.Web.Common;
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
///
///
///
private NavigationManager _NavigationManager;
///
///
///
private ClaimsIdentity identity { get; set; } = new ClaimsIdentity();
public CustomAuthenticationStateProvider(NavigationManager NavigationManager)
{
_NavigationManager = NavigationManager;
}
///
/// 用户认证成功,创建用户的ClaimsIdentity
///
///
///
public async Task SignIn(SystemUser user)
{
var claims = new[] {
new Claim(ClaimTypes.Name, user.Account),
new Claim("UserId", user.Id.ToString()),
};
identity = new ClaimsIdentity(claims, "user");
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
return true;
}
///
/// 退出登录
///
///
public async Task SignOut()
{
// 用户认证成功,创建用户的ClaimsIdentity
identity = new ClaimsIdentity();
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
return true;
}
public override async Task GetAuthenticationStateAsync()
{
//获取当前路由
var currentRoute = _NavigationManager.ToBaseRelativePath(_NavigationManager.Uri);
var user = new ClaimsPrincipal(identity);
return new AuthenticationState(user);
}
}