Blazor客户端身份验证登录针对SQLServer数据库中的User表格(我知道这是不好的实践,但这只是一个测试)。
创始人
2024-12-21 10:30:25
0

1.创建一个包含用户登录信息的用户表格。例如:

CREATE TABLE [dbo].[Users]( [Id] [int] IDENTITY(1,1) NOT NULL, [Username] nvarchar NOT NULL, [Password] nvarchar NOT NULL, [Role] [int] NOT NULL, CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([Id] ASC) )

2.在Blazor应用程序中使用ASP.NET Core Identity进行身份验证。

3.在客户端项目中添加以下包:

Microsoft.AspNetCore.Components.WebAssembly.Authentication

4.在程序启动的时候,将appsettings.json文件中的用户登录信息加载到内存中。例如:

builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) .AddHttpMessageHandler(sp => { var handler = sp.GetService() .ConfigureHandler( authorizedUrls: new[] { "https://localhost:5001" }, scopes: new[] { "ServerAPI" }); return handler; });

builder.Services.AddSingleton( new HttpClient( new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }) { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

// Load user login information from appsettings.json. var userSection = Configuration.GetSection("User"); if (userSection.Exists()) { var users = userSection.Get>(); if (users != null) { foreach (var user in users) { builder.Services.AddSingleton(user); } } }

5.在Blazor的页面中使用AuthenticationStateProvider获取用户的身份信息。例如:

@using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using Microsoft.AspNetCore.Components.Authorization @inject AuthenticationStateProvider AuthenticationStateProvider

@code { private bool isAuthenticated; private string username; private string password;

protected override async Task OnInitializedAsync()
{
    var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
    isAuthenticated

相关内容

热门资讯

突发!hhpoker透视脚本下... 突发!hhpoker透视脚本下载,微信微乐辅助器使用教程,分享透视(确实有挂)-哔哩哔哩1)hhpo...
透视总结!aapoker脚本,... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
突发!wepoker辅助器最新... 突发!wepoker辅助器最新版本更新内容,微信微乐自建房辅助工具,揭幕透视(详细教程)-哔哩哔哩1...
透视总结!hhpoker视频巡... 透视总结!hhpoker视频巡查真的假的,hh poker辅助器先试用(透视)确实有神器(哔哩哔哩)...
今年以来!epoker免费透视... 今年以来!epoker免费透视脚本,微乐小程序脚本入口,开挂脚本(揭秘有挂)-哔哩哔哩1、epoke...
透视解迷!hhpoker辅助器... 透视解迷!hhpoker辅助器,hhpoker透视脚本(透视)确实存在有神器(哔哩哔哩)1、让任何用...
为了进一步!pokerrrr2... 您好,pokerrrr2辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
脚本辅助!aapoker免费透... 脚本辅助!aapoker免费透视脚本,aapoker怎么设置抽水(透视)都是真的是有方法(哔哩哔哩)...
此事迅速冲上热搜!hh pok... 此事迅速冲上热搜!hh poker软件,微信微乐辅助器免费安装,解谜透视(新版有挂)-哔哩哔哩1、h...
脚本专业!德普之星透视辅助软件... 脚本专业!德普之星透视辅助软件,德普之星的辅助工具介绍(透视)总是有方法(哔哩哔哩)1、实时透视辅助...