Blazor SSR/WASM IDS/OIDC 单点登录授权实例1-建立和配置IDS身份验证服务

本文主要是介绍Blazor SSR/WASM IDS/OIDC 单点登录授权实例1-建立和配置IDS身份验证服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录:

  1. OpenID 与 OAuth2 基础知识
  2. Blazor wasm Google 登录
  3. Blazor wasm Gitee 码云登录
  4. Blazor SSR/WASM IDS/OIDC 单点登录授权实例1-建立和配置IDS身份验证服务
  5. Blazor SSR/WASM IDS/OIDC 单点登录授权实例2-登录信息组件wasm
  6. Blazor SSR/WASM IDS/OIDC 单点登录授权实例3-服务端管理组件
  7. Blazor SSR/WASM IDS/OIDC 单点登录授权实例4 - 部署服务端/独立WASM端授权
  8. Blazor SSR/WASM IDS/OIDC 单点登录授权实例5 - Blazor hybird app 端授权
  9. Blazor SSR/WASM IDS/OIDC 单点登录授权实例5 - Winform 端授权

源码

BlazorOIDC/Server

1. 建立 BlazorOIDC 工程

新建wasm工程 BlazorOIDC

  • 框架: 7.0
  • 身份验证类型: 个人账户
  • ASP.NET Core 托管

2. 添加自定义身份实体类,扩展IDS字段

BlazorOIDC.Server项目

编辑 Models/WebAppIdentityUser.cs 文件

using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;namespace BlazorOIDC.Server.Models;public class ApplicationUser : IdentityUser
{ /// <summary>/// Full name/// </summary>[Display(Name = "全名")][PersonalData]public string? Name { get; set; }/// <summary>/// Birth Date/// </summary>[Display(Name = "生日")][PersonalData]public DateTime? DOB { get; set; }[Display(Name = "识别码")]public string? UUID { get; set; }[Display(Name = "外联")]public string? provider { get; set; }[Display(Name = "税号")][PersonalData]public string? TaxNumber { get; set; }[Display(Name = "街道地址")][PersonalData]public string? Street { get; set; }[Display(Name = "邮编")][PersonalData]public string? Zip { get; set; }[Display(Name = "县")][PersonalData]public string? County { get; set; }[Display(Name = "城市")][PersonalData]public string? City { get; set; }[Display(Name = "省份")][PersonalData]public string? Province { get; set; }[Display(Name = "国家")][PersonalData]public string? Country { get; set; }[Display(Name = "类型")][PersonalData]public string? UserRole { get; set; }
}

3. 添加自定义声明

BlazorOIDC.Server项目

新建 Data/ApplicationUserClaimsPrincipalFactory.cs 文件

using BlazorOIDC.Server.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System.Security.Claims;namespace Densen.Models.ids;public class ApplicationUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{public ApplicationUserClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> role,IOptions<IdentityOptions> optionsAccessor) : base(userManager, role, optionsAccessor){}protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user){ClaimsIdentity claims = await base.GenerateClaimsAsync(user);var roles = await UserManager.GetRolesAsync(user);foreach (var role in roles){claims.AddClaim(new Claim("roleVIP", role));}return claims;}}

4. 配置文件

BlazorOIDC.Server项目

引用 Microsoft.EntityFrameworkCore.Sqlite 包, 示例使用sqlite数据库演示
引用第三方登录包

Microsoft.AspNetCore.Authentication.Facebook
Microsoft.AspNetCore.Authentication.Google
Microsoft.AspNetCore.Authentication.MicrosoftAccount
Microsoft.AspNetCore.Authentication.Twitter
AspNet.Security.OAuth.GitHub

编辑配置文件 appsettings.json, 添加连接字符串和第三方登录ClientId/ClientSecret等配置

{"ConnectionStrings": {"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-BlazorOIDC.Server-e292861d-0c29-45ea-84b1-b4558d5aa35d;Trusted_Connection=True;MultipleActiveResultSets=true","IdsSQliteConnection": "Data Source=../ids_api.db;"},"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"IdentityServer": {"Clients": {"BlazorOIDC.Client": {"Profile": "IdentityServerSPA"}}},"AllowedHosts": "*","Authentication": {"Google": {"Instance": "https://accounts.google.com/o/oauth2/v2/auth","ClientId": "ClientId","ClientSecret": "ClientSecret","CallbackPath": "/signin-google"},"Facebook": {"AppId": "AppId","AppSecret": "AppSecret"},"Microsoft": {"ClientId": "ClientId","ClientSecret": "ClientSecret"},"Twitter": {"ConsumerAPIKey": "ConsumerAPIKey","ConsumerSecret": "ConsumerSecret"},"Github": {"ClientID": "ClientID","ClientSecret": "ClientSecret"},"WeChat": {"AppId": "AppId","AppSecret": "AppSecret"},"QQ": {"AppId": "AppId","AppKey": "AppKey"}}
}

5. 配置IDS身份验证服务

BlazorOIDC.Server项目

编辑 Program.cs 文件

using BlazorOIDC.Server.Data;
using BlazorOIDC.Server.Models;
using Densen.Identity.Areas.Identity;
using Densen.Models.ids;
using Duende.IdentityServer;
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;var builder = WebApplication.CreateBuilder(args);// Add services to the container.
//var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
//builder.Services.AddDbContext<ApplicationDbContext>(options =>
//    options.UseSqlServer(connectionString));//EF Sqlite 配置
builder.Services.AddDbContext<ApplicationDbContext>(o => o.UseSqlite(builder.Configuration.GetConnectionString("IdsSQliteConnection")));builder.Services.AddDatabaseDeveloperPageExceptionFilter();//附加自定义用户声明到用户主体
builder.Services.AddScoped<ApplicationUserClaimsPrincipalFactory>();builder.Services.AddDefaultIdentity<ApplicationUser>(o =>
{   // Password settings.o.Password.RequireDigit = false;o.Password.RequireLowercase = false;o.Password.RequireNonAlphanumeric = false;o.Password.RequireUppercase = false;o.Password.RequiredLength = 1;o.Password.RequiredUniqueChars = 1;
}).AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddClaimsPrincipalFactory<ApplicationUserClaimsPrincipalFactory>();builder.Services.AddIdentityServer(options =>
{options.LicenseKey = builder.Configuration["IdentityServerLicenseKey"];options.Events.RaiseErrorEvents = true;options.Events.RaiseInformationEvents = true;options.Events.RaiseFailureEvents = true;options.Events.RaiseSuccessEvents = true;
}).AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>{options.IdentityResources["openid"].UserClaims.Add("roleVIP");// Client localhostvar url2 = "localhost";var spaClient2 = ClientBuilder.SPA("BlazorWasmIdentity.Localhost").WithRedirectUri($"https://{url2}:5001/authentication/login-callback").WithLogoutRedirectUri($"https://{url2}:5001/authentication/logout-callback").WithScopes("openid Profile").Build();spaClient2.AllowOfflineAccess = true;spaClient2.AllowedCorsOrigins = new[]{$"https://{url2}:5001"};options.Clients.Add(spaClient2);//2024-1-23 更新测试端点配置项var spaClientBlazor5002 = ClientBuilder.SPA("Blazor5002").WithScopes("api").Build();spaClientBlazor5002.AllowedCorsOrigins = new[]{$"http://0.0.0.0",$"http://0.0.0.0:5001",$"http://0.0.0.0:5002",$"http://localhost",$"http://localhost:5001",$"http://localhost:5002",$"https://localhost",$"https://localhost:5001",$"https://localhost:5002"};foreach (var item in spaClientBlazor5002.AllowedCorsOrigins){spaClientBlazor5002.RedirectUris.Add($"{item}/authentication/login-callback");spaClientBlazor5002.PostLogoutRedirectUris.Add($"{item}/authentication/logout-callback");}spaClientBlazor5002.AllowOfflineAccess = true;options.Clients.Add(spaClientBlazor5002);});builder.Services.AddAuthentication();var autbuilder = new AuthenticationBuilder(builder.Services);
autbuilder.AddGoogle(o =>
{o.ClientId = builder.Configuration["Authentication:Google:ClientId"] ?? "";o.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"] ?? "";o.ClaimActions.MapJsonKey("urn:google:profile", "link");o.ClaimActions.MapJsonKey("urn:google:image", "picture");
});
//autbuilder.AddFacebook(o =>
//{
//    o.AppId = builder.Configuration["Authentication:Facebook:AppId"] ?? "";
//    o.AppSecret = builder.Configuration["Authentication:Facebook:AppSecret"] ?? "";
//});
//autbuilder.AddTwitter(o =>
//{
//    o.ConsumerKey = builder.Configuration["Authentication:Twitter:ConsumerAPIKey"] ?? "";
//    o.ConsumerSecret = builder.Configuration["Authentication:Twitter:ConsumerSecret"] ?? "";
//    o.RetrieveUserDetails = true;
//});
autbuilder.AddGitHub(o =>
{o.ClientId = builder.Configuration["Authentication:Github:ClientID"] ?? "";o.ClientSecret = builder.Configuration["Authentication:Github:ClientSecret"] ?? "";
});
//autbuilder.AddMicrosoftAccount(o =>
//{
//    o.ClientId = builder.Configuration["Authentication:Microsoft:ClientId"] ?? "";
//    o.ClientSecret = builder.Configuration["Authentication:Microsoft:ClientSecret"] ?? "";
//});
//if (WeChat) autbuilder.AddWeChat(o =>
//{
//    o.AppId = Configuration["Authentication:WeChat:AppId"];
//    o.AppSecret = Configuration["Authentication:WeChat:AppSecret"];
//    o.UseCachedStateDataFormat = true;
//})
//autbuilder.AddQQ(o =>
//{
//    o.AppId = builder.Configuration["Authentication:QQ:AppId"] ?? "";
//    o.AppKey = builder.Configuration["Authentication:QQ:AppKey"] ?? "";
//});
autbuilder.AddOpenIdConnect("oidc", "Demo IdentityServer", options =>
{options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;options.SignOutScheme = IdentityServerConstants.SignoutScheme;options.SaveTokens = true;options.Authority = "https://demo.duendesoftware.com";options.ClientId = "interactive.confidential";options.ClientSecret = "secret";options.ResponseType = "code";options.TokenValidationParameters = new TokenValidationParameters{NameClaimType = "name",RoleClaimType = "role"};
});builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();var app = builder.Build();// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{app.UseMigrationsEndPoint();app.UseWebAssemblyDebugging();
}
else
{app.UseExceptionHandler("/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();
}app.UseHttpsRedirection();app.UseBlazorFrameworkFiles();
app.UseStaticFiles();app.UseRouting();
app.UseCors(o => o.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());app.UseIdentityServer();
app.UseAuthorization();app.MapBlazorHub();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");app.Run();

6. 运行工程

因为篇幅的关系,具体数据库改为sqlite生成脚本步骤参考以前文章或者直接拉源码测试

  • 点击注册按钮
  • 用户名 test@test.com
  • 密码 1qaz2wsx

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 点击 Apply Migrations 按钮

  • 刷新页面

  • 已经可以成功登录

这篇关于Blazor SSR/WASM IDS/OIDC 单点登录授权实例1-建立和配置IDS身份验证服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/697228

相关文章

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.