开发者

default login url on HttpUnauthorizedResult in asp.net mvc

开发者 https://www.devze.com 2023-03-06 16:40 出处:网络
I have written a custom AuthorizeAttribute which has the following condition in asp.net mvc3 application:

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.Ide开发者_运维问答ntity.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..

default login url on HttpUnauthorizedResult in asp.net mvc

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


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" />


You should be modifying the root one for loginUrl.

i have created AuthorizationAttribute... it's redirecting properly e.g.

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

and my attribute is:

public class AuthorizationAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);

        if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}

and apply attribute to any method of your controller as necessary...

[AuthorizationAttribute()]
public ActionResult Index()
{
    return View();
}


I recently had this problem and found it was because I had the WebMatrix.dll referenced in my project.

Removing this DLL fixed the issue

see here What is the PreserveLoginUrl appSetting key/value in an ASP.NET MVC application?

0

精彩评论

暂无评论...
验证码 换一张
取 消