首页 > 其他分享 >Blazor SSR/WASM IDS/OIDC 单点登录授权实例讲解1

Blazor SSR/WASM IDS/OIDC 单点登录授权实例讲解1

时间:2024-01-17 09:15:46浏览次数:20  
标签:Microsoft builder IDS SSR Authentication WASM using app options

目录:

  1. OpenID 与 OAuth2 基础知识
  2. Blazor wasm Google 登录
  3. Blazor wasm Gitee 码云登录
  4. Blazor SSR/WASM IDS/OIDC 单点登录授权实例讲解1
  5. Blazor SSR/WASM IDS/OIDC 单点登录授权实例讲解2
  6. Blazor SSR/WASM IDS/OIDC 单点登录授权实例讲解3

源码

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 localhost
        var 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);
    });

 
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生成脚本步骤参考以前文章或者直接拉源码测试

  • 点击 Apply Migrations 按钮

  • 刷新页面

  • 已经可以成功登录

标签:Microsoft,builder,IDS,SSR,Authentication,WASM,using,app,options
From: https://www.cnblogs.com/densen2014/p/17959982

相关文章

  • Androidstudio copilot 安装
    如何安装AndroidStudioCopilot1.安装AndroidStudio首先,你需要安装AndroidStudio,这是一个用于开发Android应用程序的集成开发环境(IDE)。你可以从官方网站(Studio,并按照安装向导的指示进行安装。2.配置AndroidStudio安装完成后,你需要对AndroidStudio进行一些基本配置。打......
  • Androidstudio如何使用Copilot
    AndroidStudio如何使用Copilot引言AndroidStudio是一款功能强大的集成开发环境(IDE),提供了丰富的工具和功能来帮助开发者创建和调试Android应用程序。Copilot是GitHub开发的人工智能代码助手,可以为开发者提供代码自动补全和提示。本文将介绍如何在AndroidStudio中使用Copilot,以......
  • [Rust] Rust开发Wasm运行到支付宝小程序
    Rust开发Wasm运行到支付宝小程序最近参加了支付宝小程序开发者大赛,把我之前的RustNES项目(任天堂红白机游戏机模拟器,可以玩小霸王上的马里奥,冒险岛等)迁移到了支付宝小程序上,发现相关内容网上的资料比较匮乏,在此分享一下开发经历.本项目两大核心点Rust编译成Wasm运......
  • wasmex webassenbly elixir 运行时
    wasmex是基于wasmtime以及rustnif开发的方便elixir运行webassembly的框架与rust的集成与rust集成使用的三方包 与mjml工具类似使用了rustler_precompiled以及rustlerrust使用的三方包 前边也说了是基于了wasmtime包装的,同时使用了wasmtimewasi一些子模块说明rustle......
  • 安卓期末大作业(AndroidStudio开发),垃圾分类app,代码有注释,能正常运行
    1 项目基本信息1.1 项目名称垃圾分类助手APP的设计与实现1.2开发运行环境开发语言:Java开发工具:AndroidStudio模拟器:雷电模拟器9数据库:SQLite  1.3使用的核心类及组件Activity:作为实现界面的窗体类BaseAdapter:适配器类SqliteOpenHelper:数据库类Intent:页面跳转传值Fragmen......
  • 安卓期末大作业(AndroidStudio开发),日记本app,代码注释详细,能正常运行
    安卓期末大作业-日记本app(附下载链接)压缩包内包含源代码,运行各个界面截图,一条日记可以记录2000字符以下的文本、最多8张配图和最多8个视频。每条日记都可以以评论的形式或转发引用的形式追更,评论的最大长度也是2000字符。日记还可以同时记录所处位置和当时的天气情况(当然,现在并......
  • Android期末大作业:使用AndroidStudio开发图书管理系统APP(使用sqlite数据库)
    AndroidStudio开发项目图书管理系统项目视频展示:引言现在是一个信息高度发达的时代,伴随着科技的进步,文化的汲取,人们对于图书信息的了解与掌握也达到了一定的高度。尤其是学生对于知识的渴求更是与日俱增。图书馆作为学生学习知识的重要场所,作为信息资源的集散地,图书和用户借阅资......
  • 《安卓期末大作业(AndroidStudio开发),垃圾分类app,代码有注释,能正常运行》
    1 项目基本信息1.1 项目名称垃圾分类助手APP的设计与实现1.2开发运行环境开发语言:Java开发工具:AndroidStudio模拟器:雷电模拟器9数据库:SQLite  1.3使用的核心类及组件Activity:作为实现界面的窗体类BaseAdapter:适配器类SqliteOpenHelper:数据库类Intent:页面跳转传值Fragmen......
  • 安卓期末大作业(AndroidStudio开发),日记本app,代码注释详细,能正常运行
    安卓期末大作业-日记本app(附下载链接)压缩包内包含源代码,运行各个界面截图,一条日记可以记录2000字符以下的文本、最多8张配图和最多8个视频。每条日记都可以以评论的形式或转发引用的形式追更,评论的最大长度也是2000字符。日记还可以同时记录所处位置和当时的天气情况(当然,现在并......
  • 《Android期末大作业:使用AndroidStudio开发图书管理系统APP(使用sqlite数据库)》
    AndroidStudio开发项目图书管理系统项目视频展示:引言现在是一个信息高度发达的时代,伴随着科技的进步,文化的汲取,人们对于图书信息的了解与掌握也达到了一定的高度。尤其是学生对于知识的渴求更是与日俱增。图书馆作为学生学习知识的重要场所,作为信息资源的集散地,图书和用户借阅资......