大佬教程收集整理的这篇文章主要介绍了Blazor WebAssembly+IdentityServer4,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本例子使用的是Blazor WebAssembly+IdentityServer4+Sql Server
IdentityServer4的基础知识可以访问官方网站学习:https://identityserver4.readthedocs.io/en/latest/
一.创建IdentityServer4
1.创建名为WebAppSample的空白解决方案。
2.通过命令安装IdentityServer4的模板,VS->工具->命令行->开发者命令提示,执行:dotnet new -i IdentityServer4.Templates,安装成功提示如下:
3.创建IdentityServer4项目,我这里使用的是IdentityServer4+Asp.Net Core Identity,以Asp.Net Core Identity作为IdentityServer4的用户支持。
4.把项目WebApp.IdentityServer的目标框架改为:.NET 5.0。
5.设置数据库上下文,这里我们使用的是SQL Server数据库。
{ "ConnectionStrings": { "DefaultConnection": "Server=127.0.0.1;Initial Catalog=WebAppIdentityServerDB;Persist Security Info=false;User id=sa;password=sa123456;MultipleActiveResultSets=true" } }
public class Datas { public static String DbConnectionString { get; set; } = "Server=127.0.0.1;" + "Initial Catalog=WebAppIdentityServerDB;" + "Persist Security Info=false;" + "User id=sa;" + "password=sa123456;" + "@H_882_60@multipleActiveResultSets=true"; }
public Startup(IConfiguration configuration) { Configuration = configuration; Datas.DbConnectionString = Configuration.GetConnectionString("DefaultConnection"); }
services.AddDbContext<ApplicationDbContext>(options => optionS.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
6.配置将客户端(Client)、资源(Identityresource、Apiresource、ApiScope)、操作数据(codes、tokens、consents)保存至数据库。
@H_114_197@
var builder = services.AddIdentityServer(options => { options.Events.RaiseErrorEvents = true; options.Events.RaiseInformationEvents = true; options.Events.RaiseFailureEvents = true; options.Events.RaisesuccessEvents = true; // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html options.EmitStaticAudienceClaim = true; }); #region 保存在内存中的身份资源、API资源、客户端资源 //identityServerBuilder.AddInMemoryIdentityresources(Config.Identityresources); //identityServerBuilder.AddInMemoryApiScopes(Config.ApiScopes); //identityServerBuilder.AddInMemoryClients(Config.Clients); #endregion #region 保存在数据库中的身份资源、API资源、客户端资源 //配置数据(客户端、资源)至数据库:ConfigurationDbContext //typeof(SeedData).Assembly.FullName builder.AddConfigurationStore(options => { options.ConfigureDbContext = builder => { builder.UseSqlServer(Datas.DbConnectionString, b => b.MigrationsAssembly("WebApp.IdentityServer")); }; }); //配置操作数据(codes、tokens、consents)至数据库:PersistedGrantDbContext builder.AddoperationalStore(options => { options.ConfigureDbContext = builder => { builder.UseSqlServer(Datas.DbConnectionString, b => b.MigrationsAssembly("WebApp.IdentityServer")); }; //启用自动令牌清理,这是可选的;清理时间默认是3600秒。 options.EnableTokenCleanup = true; options.TokenCleanupInterval = 3600; });
7.配置IdentityServer使用Asp.Net Core Identity的用户作为用户支持。
builder.AddAspNetIdentity<ApplicationUser>();
8.注册自定义用户声明工厂,用于添加自定义用户声明到用户标识,例如用户权限。
@H_114_197@
public class AdditionalUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> { public AdditionalUserClaimsPrincipalFactory(UseRMANager<ApplicationUser> useRMANager, RoleManager<IdentityRole> roleManager, IOptions<IdentityOptions> optionsAccessor) : base(useRMANager, roleManager, optionsAccessor) { } public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user) { var principal = await base.CreateAsync(user); var identity = (ClaimsIdentity)principal.Identity; var claims = new List<Claim>(); claims.Add(new Claim(JwtClaimTypes.PreferredUserName, user.UserName)); //你的代码:从数据库读取用户的权限声明 identity.AddClaims(claims); return principal; } }
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AdditionalUserClaimsPrincipalFactory>();
二.创建Blazor WebAssembly
以上是大佬教程为你收集整理的Blazor WebAssembly+IdentityServer4全部内容,希望文章能够帮你解决Blazor WebAssembly+IdentityServer4所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。