asp.net core 3.0 身份认证 替换为自已的提供程序 AuthenticationStateProvider replace to SelfAuthenticationStateProvider
2024-09-04 21:37:40
public void ConfigureServices(IServiceCollection services)
{
// 添加身份验证服务
services.AddAuthorizationCore();
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
}
using BlazorDemo.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks; namespace BlazorDemo.Client
{
public class SelfAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly HttpClient _httpClient; public SelfAuthenticationStateProvider(HttpClient httpClient)
{
_httpClient = httpClient;
} public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var userInfo = await _httpClient.GetJsonAsync<UserInfo>("user"); var identity = userInfo.IsAuthenticated
? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userInfo.Name) }, "selfauth")
: new ClaimsIdentity(); return new AuthenticationState(new ClaimsPrincipal(identity));
}
}
}
最新文章
- flask+sqlite3+echarts2+ajax数据可视化报错:UnicodeDecodeError: &#39;utf8&#39; codec can&#39;t decode byte解决方法
- 实现滑动可固定header以及页面刷新
- 利用IIS管理器模拟CDN
- testng xml中按顺序执行java类
- Android WebView加载本地html并实现Java与JS交互
- 使用MulticastSocket实现多点广播
- 树莓派3 安装ubuntu-mate
- isIsomorphic
- [iOS基础控件 - 6.11.1] - 控制器 &; 控制器view
- xenserver 备份backup和还原restore命令
- laravel5的坑
- QTableView表格滚动条样式(QSS真是细致到家了)
- #翻译#原文来自Database.System.Concepts(6th.Edition.2010)2.6Relational Operations,原文作者Abraham Silberschaz , Henry F. Korth , S. Sudarshan
- Elastic 之倒排索引(二)
- boostrap 日期插件(带中文显示)
- 排序算法<;No.4>;【基数排序】
- [BZOJ2834]回家的路
- supervisor安装与问题
- 使用AngularJS 添加行修改、删除表格数据
- Java并发多线程 - 并发工具类JUC