本文转自:http://quabr.com/40446028/how-to-override-handleunauthorizedrequest-in-asp-net-core

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<CookieAuthenticationOptions>(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 Darkonekt

    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<CustomAuthorizationAttribute>();
    }

最新文章

  1. BZOJ3160万径人踪灭
  2. Linux操作系统PS命令详细解析
  3. python pexpect 学习与探索
  4. static之用法
  5. Orchard Express Oracle v1.7.2 发布
  6. oracle数据库中varchar2陷阱
  7. DNA电荷转移:电阻的计算公式 &amp; Marcus电子转移理论
  8. input输入密码变黑点密文
  9. C语言-数据的快速引用
  10. Linux中ctrl+z 、ctrl+c、 ctrl+d区别
  11. webstorm中github的配置
  12. MySQL安装与启动——Windows系统下
  13. day 34
  14. zabbix图形乱码问题解决办法
  15. Storm——Android SQLite数据库管理类库
  16. Java中byte、short、char、int、long运算时自动类型转化问题
  17. spring cloud config服务器
  18. 学习笔记之Bokeh Data Visualization | DataCamp
  19. Docker环境的持续部署优化实践
  20. Caffe+Windows 环境搭建收集

热门文章

  1. MongoDB 安全和访问权限控制
  2. [.Net] 手把手带你将自己打造的类库丢到 NuGet 上
  3. 【NLP】揭秘马尔可夫模型神秘面纱系列文章(五)
  4. 10.JAVA之GUI编程弹出对话框Dialog
  5. 深入理解CSS动画animation
  6. 基于 HTML5 的 WebGL 技术构建 3D 场景(一)
  7. Kafka无消息丢失配置
  8. form表单的字符串进行utf-8编码
  9. NSwagStudio for Swagger Api
  10. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)