欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > Core 授权 认证 策略案例

Core 授权 认证 策略案例

2025/2/8 0:43:35 来源:https://blog.csdn.net/u013400314/article/details/144141502  浏览:    关键词:Core 授权 认证 策略案例

利用 cookie 模式 》》 框架默认的

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

利用 cookie 模式 》》 策略授权

》》》策略对应的处理逻辑,这里可以请求action时,如果添加了策略,则会触发对应的策略处理模块

using Microsoft.AspNetCore.Authorization;namespace ZenAuth.Coms
{public class CustomAuthorizationHandler : AuthorizationHandler<CustomRequirement>{private readonly IStudent _studentService;//可以实现注入public CustomAuthorizationHandler(IStudent studentService){_studentService = studentService;}protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomRequirement requirement){// 使用 _customService 进行处理this._studentService.Write();Console.WriteLine("");          if (1 == 1){// 验证成功context.Succeed(requirement);}return Task.CompletedTask;}}
}

》》》添加策略对应的 参数

using Microsoft.AspNetCore.Authorization;namespace ZenAuth.Coms
{public class CustomRequirement: IAuthorizationRequirement{// 可以在这里定义需要的任何属性或方法public CustomRequirement(){}}
}

》》》
在这里插入图片描述

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.BearerToken;
using ZenAuth.Coms;
using Microsoft.AspNetCore.Authorization;namespace ZenAuth
{public class Program{public static void Main(string[] args){var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllersWithViews();builder.Services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();builder.Services.AddSingleton<IStudent, Student>();#region 认证builder.Services.AddAuthentication(options =>{options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultForbidScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;}).AddCookie(options =>{//登陆地址options.LoginPath = "/Account/Index";//禁止访问地址options.AccessDeniedPath = "/Account/Index";//Cookie过期时间options.ExpireTimeSpan = TimeSpan.FromMinutes(20);//校验过期});#endregion#region 授权// 默认的模式//builder.Services.AddAuthorization();//授权策略模式 扩展 builder.Services.AddAuthorization(options =>{options.AddPolicy("CustomPolicy", policy =>{policy.Requirements.Add(new CustomRequirement());});});#endregionvar app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();app.UseRouting();#region 认证和授权中间件//app.UseAuthentication();app.UseAuthorization();#endregionapp.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");app.Run();}}
}

在这里插入图片描述
源码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com