如何更改与控制反转原理兼容的代码?

时间:2017-02-02 20:20:53

标签: c# asp.net oop inversion-of-control

我在我的网络应用程序中使用AuthorzieAttribute进行身份验证和授权。 如何更改与控制反转原理兼容的HandleUnauthorizedRequest方法(IOC)?

public class AuthorizeUser : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return SessionContext.GetCurrentUser() != null ? true : false;
    }
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary(
                        new
                        {
                            returnUrl = filterContext.HttpContext.Request.Url,
                            Area = "",
                            controller = "Account",
                            action = "Login"
                        })
                    );
    }
}

1 个答案:

答案 0 :(得分:0)

答案:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
     filterContext.HttpContext.Response.RedirectToRoute(new { controller = "Account", action = "Login", returnUrl = filterContext.HttpContext.Request.Url });
}