default login url on HttpUnauthorizedResult in asp.net mvc

好久不见. 提交于 2019-12-04 03:45:14

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

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

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!