Asp.net-mvc – default login url on HttpUnauthorizedResult in asp.net mvc

asp.net-mvcauthenticationcustom-attributesforms-authentication

I have written a custom AuthorizeAttribute which has the following condition in asp.net mvc3 application:

public override void OnAuthorization(AuthorizationContext filterContext)
{     
    //auth failed, redirect to Sign In
    if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
       filterContext.Result = new HttpUnauthorizedResult();
    }
}

And in my web.config, i have:

<authentication mode="Forms">
  <forms loginUrl="~/User/SignIn" timeout="2880" />
</authentication>

On authentication fail, it redirects to "/Account/Login" page by default.

How do i change this default redirect url and redirect it to "/User/SignIn"?

The screenshot shows the clear view of what i am trying to say..HttpUnauthorizedresult image

Though i have set '/User/SignIn', it redirects to '/Account/Login'

Best Answer

I am not sure whether i can add this as an answer. But this may help others who were having this related issue.

I got the solution after a struggle. I have added WebMatrix.WebData reference recently, which seems to be the real culprit of this issue. This can be handled by adding the key to your config file:

<add key="loginUrl" value="~/User/SignIn" />