Blazor OIDC 单点登录授权实例2-登录信息组件wasm
目录:
- OpenID 与 OAuth2 基础知识
- Blazor wasm Google 登录
- Blazor wasm Gitee 码云登录
- Blazor OIDC 单点登录授权实例1-建立和配置IDS身份验证服务
- Blazor OIDC 单点登录授权实例2-登录信息组件wasm
- Blazor OIDC 单点登录授权实例3-服务端管理组件
- Blazor OIDC 单点登录授权实例4 - 部署服务端/独立WASM端授权
- Blazor OIDC 单点登录授权实例5 - 独立SSR App (net8 webapp)端授权
- Blazor OIDC 单点登录授权实例6 - Winform 端授权
- Blazor OIDC 单点登录授权实例7 - Blazor hybird app 端授权
(目录暂时不更新,跟随合集标题往下走)
源码
1. 编辑 BlazorOIDC.Shared 项目配置文件
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.*" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.*" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.*" />
<PackageReference Include="BootstrapBlazor" Version="8.*" />
<PackageReference Include="FreeSql" Version="3.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.AspNetCore.Components" />
<Using Include="Microsoft.Extensions.Options" />
<Using Include="Microsoft.JSInterop" />
<Using Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>
</Project>
2. 共享工程添加登录信息组件
Pages/LogInfo.razor
@page "/logInfo"
@namespace Densen.Identity.Shared
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Identity
@inject NavigationManager Navigation
<PageTitle>登录信息</PageTitle>
<h1>登录信息</h1>
<button @onclick="LogUsername">检查登录信息</button>
<p>@authMessage</p>
@code
{
[Inject]
[NotNull]
protected IJSRuntime? jsRuntime { get; set; }
private string LoginUrl = "Identity/Account/Login";
private string LogoutUrl = "Identity/Account/Register";
private string RegisterUrl = "Identity/Account/Register";
private string? claimInfo;
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
if (this.jsRuntime is IJSInProcessRuntime)
{
LoginUrl = "authentication/login";
LogoutUrl= "authentication/logout";
RegisterUrl = "authentication/register";
StateHasChanged();
}
}
}
/// <summary>
/// 级联参数获取身份验证状态数据
/// </summary>
[CascadingParameter]
private Task<AuthenticationState>? authenticationStateTask { get; set; }
private string? authMessage;
private async Task LogUsername()
{
var authState = await authenticationStateTask!;
var user = authState.User;
if (user!.Identity!.IsAuthenticated)
{
authMessage = $"{user.Identity.Name} is authenticated.";
}
else
{
authMessage = "The user is NOT authenticated.";
}
}
private void BeginLogOut()
{
Navigation.NavigateTo(LogoutUrl, forceLoad: true);
}
}
<p>AuthorizeView 组件根据用户是否获得授权来选择性地显示 UI 内容。 如果只需要为用户显示数据,而不需要在过程逻辑中使用用户的标识,那么此方法很有用</p>
<AuthorizeView>
<Authorized>
你好, @context.User.Identity?.Name
[
@if (@context.User.IsInRole(AuthorizeRoles.Admin.ToString()))
{
<span>管理员 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.Superuser.ToString()))
{
<span>超级用户 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.User.ToString()))
{
<span>用户 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.R110.ToString()))
{
<span>R110用户 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.R120.ToString()))
{
<span>R120用户 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.R130.ToString()))
{
<span>R130用户 </span>
}
@if (@context.User.IsInRole(AuthorizeRoles.R140.ToString()))
{
<span>R140用户 </span>
}
]
<br /><br /><br />
<h5>以下是用户的声明</h5><br />
@foreach (var claim in context.User.Claims)
{
<p>@claim.Type: @claim.Value</p>
}
<p>@claimInfo</p>
<button type="submit" class="nav-link btn btn-link" @onclick="BeginLogOut">注销</button>
</Authorized>
<NotAuthorized>
<span>看起来你还没登录</span>
<a href="@RegisterUrl">注册</a>
<a href="@LoginUrl">登录</a>
</NotAuthorized>
</AuthorizeView>
<p>以下是基于角色或基于策略的授权,未登录不显示 </p>
<AuthorizeView Roles="Admin, Superuser">
<p>只有管理员或超级用户才能看到.</p>
</AuthorizeView>
3. 添加共享引用文件
_Imports.razor
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using System.Diagnostics.CodeAnalysis
@using Densen.Identity
@using Densen.Identity.Shared
4. 编辑功能菜单
BlazorOIDC.Client 项目 Shared/NavMenu.razor 文件
源码
<div class="nav-item px-3">
<NavLink class="nav-link" href="logInfo">
<span class="oi oi-plus" aria-hidden="true"></span> 登录信息
</NavLink>
</div>
5. 添加 Shared 工程的页面路由
BlazorOIDC.Client 项目 App.razor 文件
Router 添加 BlazorOIDC.Shared 工程的页面路由
<Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="new[] { typeof(BlazorOIDC.Shared._Imports).Assembly }">
6. 运行项目
点击登录, 然后点击新添加的页面, 测试有效
7. 其他
其他页面参考以前文章或者源码
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/17959984