ASP.NETCore5MVC超级管理员连接
创始人
2024-09-17 11:30:37
0
  1. 首先定义一个对应管理员页面的控制器,并添加相应的Action方法,如下所示:
public class AdminController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult SuperAdmin()
    {
        return View();
    }
}
  1. 然后,在Startup.cs文件中添加身份验证和授权服务。
public void ConfigureServices(IServiceCollection services)
{
    // 添加身份验证
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.LoginPath = "/Account/Login";
            options.AccessDeniedPath = "/Account/AccessDenied";
        });

    // 添加授权服务
    services.AddAuthorization(options =>
    {
        // 添加超级管理员策略
        options.AddPolicy("SuperAdminPolicy", policy =>
        {
            policy.RequireClaim("IsSuperAdmin");
        });
    });

    services.AddControllersWithViews();
}

其中,在AddPolicy中添加一个超级管理员策略,要求具有IsSuperAdmin声明的用户才能访问超级管理员页面。

  1. 在Admin控制器中添加一个Action方法,该方法使用SuperAdminPolicy策略限制只能超级管理员访问。
[Authorize(Policy = "SuperAdminPolicy")]
public IActionResult SuperAdmin()
{
    return View();
}
  1. 最后,在超级管理员的登录方法中添加声明IsSuperAdmin并将其值设置为true,如下所示:
public async Task Login(LoginViewModel model, string returnUrl = null)
{
    // 验证用户身份信息
    var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
    if (result.Succeeded)
    {
        // 获取当前用户信息
        var user = await _userManager.FindByNameAsync(model.UserName);
        if (user != null)
        {
            // 设置IsSuperAdmin声明为true
            var claims = new List
            {
                new Claim("IsSuperAdmin", "true")
            };
            await _userManager.AddClaims

相关内容

热门资讯

玩家攻略!竞技联盟透视插件,哈... 玩家攻略!竞技联盟透视插件,哈糖大菠萝可以开挂吗,确实真的是有挂(详细教程)1、不需要AI权限,帮助...
分享个大家!wepoker数据... 分享个大家!wepoker数据分析工具,pokerrrr2辅助,其实是有挂(真的有挂)1、辅助软件下...
三分钟了解!aapoker怎么... 三分钟了解!aapoker怎么控制牌,哈糖大菠萝开挂,原来有挂(竟然有挂)1、进入到是否有挂之后,能...
专业讨论!德州局hhpoker... 专业讨论!德州局hhpoker,wepoker辅助透视,竟然存在有挂(有挂教程)专业讨论!德州局hh...
科技揭秘!wepoker辅助透... 科技揭秘!wepoker辅助透视软件,菠萝德普辅助器免费版在哪里,本来真的是有挂(有挂教程)科技揭秘...
必备科技!德普之星透视软件免费... 必备科技!德普之星透视软件免费入口官网,wepoker网页版透视方法,其实是真的有挂(有挂方式)1、...
盘点十款!pokemmo辅助器... 盘点十款!pokemmo辅助器,德州透视是真的假的,都是真的有挂(真实有挂)1、起透看视 辅助软件价...
热点讨论!wpk刷入池率脚本,... 热点讨论!wpk刷入池率脚本,约局吧辅助器,一直是有挂(有挂功能)1、超多福利:超高返利,海量正版游...
最新研发!hhpoker有没有... 最新研发!hhpoker有没有作弊辅助,wepoker有没有插件,确实有挂(有挂秘诀)1)有没有挂:...
我来教大家!wejoker私人... 我来教大家!wejoker私人辅助软件,aapoker插件,其实是真的有挂(有挂解密)1、公共底牌简...