博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]How to override HandleUnauthorizedRequest in ASP.NET Core
阅读量:5980 次
发布时间:2019-06-20

本文共 2689 字,大约阅读时间需要 8 分钟。

本文转自:

I'm migrating my project to asp.net core and I'm stuck in migrating my CustomAuthorization attribute for my controllers. Here is my code.

public class CustomAuthorization : AuthorizeAttribute{    public string Url { get; set; }    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)    {        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)        {            filterContext.Result = new RedirectResult(Url + "?returnUrl=" + filterContext.HttpContext.Request.Url.PathAndQuery);        }        else if (!Roles.Split(',').Any(filterContext.HttpContext.User.IsInRole))        {            filterContext.Result = new ViewResult            {                ViewName = "AcessDenied"            };        }        else        {            base.HandleUnauthorizedRequest(filterContext);        }    }}

then i used it to my controllers

[CustomAuthorization(Url = "/Admin/Account/Login", Roles = "Admin")]public abstract class AdminController : Controller { }

so, basically i can use it to redirect to different login page when roles is not met. I have few areas and each of them have different login page. I tried using the CookieAuthenticationOptions like this

services.Configure
(options =>{ options.AuthenticationScheme = "Admin"; options.LoginPath = "/Admin/Account/Login";});

then on my admin controller

[Area("Admin")][Authorize(ActiveAuthenticationSchemes = "Admin", Roles = "Admin")]

but after i login, it still cant get in.

1 answer

  • answered 2016-11-06 13:17

    I am doing something similar in one of my projects.  This answer is NOT using AuthorizeAttribute; but it might help some one landing here from a google search. In my case I am using it to authorize based on custom logic.

    First my custom attribute class:

    public class CustomAuthorizationAttribute : ActionFilterAttribute{    private readonly IMyDepedency _dp;    public CustomAuthorizationAttribute(IMyDepedency dp)    {        _dp = dp;    }    public override void OnActionExecuting(ActionExecutingContext context)    {        var isValid = false;       //write my validation and authorization logic here         if(!isValid)        {            var unauthResult = new UnauthorizedResult();            context.Result = unauthResult;                        }        base.OnActionExecuting(context);    }}

    I decorate my controllers like this:

    [ServiceFilter(typeof (CustomAuthorizationAttribute))]

    Then in my Startup class

    public void ConfigureServices(IServiceCollection services){     // Add framework services.     services.AddMvc();   // my other stuff that is not relevant in this post     // Security     services.AddTransient
    (); }

 

转载地址:http://cuoox.baihongyu.com/

你可能感兴趣的文章
《Cisco IPv6网络实现技术(修订版)》一2.6 配置练习:使用Cisco路由器配置一个IPv6网络...
查看>>
《可穿戴创意设计:技术与时尚的融合》一一第2章 与可穿戴设备有关的故事...
查看>>
ruby动态new对象
查看>>
《JavaScript启示录》——导读
查看>>
如何让你的 Linux 系统干净整洁
查看>>
《JavaScript高效图形编程(修订版)》——6.10 用画布sprites取代DHTMLsprite
查看>>
Linux中grep命令的12个实践例子
查看>>
使用Docker Compose部署基于Sentinel的高可用Redis集群
查看>>
Mybatis 3学习笔记(一)
查看>>
Guice系列之用户指南(十)
查看>>
树与森林的存储、遍历和树与森林的转换
查看>>
Android自定义属性
查看>>
Visual C#之核心语言
查看>>
代码重构(五):继承关系重构规则
查看>>
Windows App开发之集合控件与数据绑定
查看>>
中大型网站技术架构演变过程
查看>>
ARTS训练第三周
查看>>
vue中v-for循环如何将变量带入class的属性名中
查看>>
ceph学习笔记之七 数据平衡
查看>>
windows下的php的memcache扩展的安装及memcache最新下载地址
查看>>