开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 355|回复: 0
收起左侧

[C#源码] asp.core 基于cook 鉴权自定义策略授权

[复制链接]
结帖率:100% (2/2)
发表于 2024-1-18 14:54:17 | 显示全部楼层 |阅读模式   湖北省黄冈市
本帖最后由 陌小北 于 2024-1-18 14:55 编辑


[C#] 纯文本查看 复制代码
using jwtAuthTest.AuthorizationExtend;
using jwtAuthTest.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;

namespace jwtAuthTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            builder.Services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;//不能少,默认的授权渠道
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultForbidScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;


            }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.LoginPath = "/Login";      //如果在鉴权的过程中,没有解析到用户的信息,就跳转到登录页
                options.AccessDeniedPath = "/AccessDeniedPath"; //解析到了用户信息,但是根据当前的用户信息判断的时候,不允许支持访问资源

            });


            //配置添加授权规则
            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("AdminPolicy", policyBuilder =>
                {

                    policyBuilder.RequireAssertion(context =>
                    {

                        if (context.User.Claims == null || context.User.Claims.Count() <= 0)
                        {
                            return false;
                        }

                       return true;
                    });

                    policyBuilder.AddRequirements(new AdminCookieRequirement(CookieAuthenticationDefaults.AuthenticationScheme));

                });



            });


            builder.Services.AddSingleton<IAuthorizationHandler, AdminCookieHandler>();
            builder.Services.AddSingleton<IUserService, UserService>();



            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthentication();
            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}

cookAuthTest.zip

1.49 MB, 下载次数: 0, 下载积分: 精币 -2 枚

售价: 1 枚 精币  [记录]  [购买]

全部源码

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表