8

我写这在asp.net MVC3应用以下条件的自定义AuthorizeAttribute默认登录网址

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

而且在我的web.config,我有:

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

验证失败后,默认情况下,它会重定向到“/帐户/登录”页面。

如何更改此默认重定向网址并将其重定向到“/用户/登录”?

的截图显示的就是我想说的清晰视野.. HttpUnauthorizedresult image

虽然我已经设置“/用户/签到”,它重定向到“/帐号/登录”

+0

? – 2011-05-17 13:18:47

+0

是的。我正在使用MVC3。 – Prasad 2011-05-17 13:29:55

+3

经过一番努力,我获得了解决方案。我最近添加了WebMatrix.WebData参考,这似乎是这个问题的真正罪魁祸首。这可以通过将密钥添加到您的配置文件来处理: Prasad 2011-05-20 08:57:33

回答

9

我不知道我是否可以将其添加为一个答案。但这可能有助于其他人有这个相关的问题。

我在挣扎之后得到了解决方案。我最近添加了WebMatrix.WebData参考,这似乎是这个问题的真正罪魁祸首。这可以通过将关键看你的配置文件进行处理:

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

该键应该添加到web.config中的appSettings部分。 – 2011-07-21 23:33:33

+0

我认为我们通常会在appSettings部分添加如上所示的键:) – Prasad 2011-07-22 04:58:56

+0

@Prasad我已经花了几个小时的时间了...感谢一群朋友在这里回复并保存了我的余生! – spender 2012-07-03 13:14:02

2

你应该修改loginUrl的根目录。

我已经创建了AuthorizationAttribute ...它正在重定向

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

和我的属性是:

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

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

和应用属性控制器需要的任何方法...

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

我已经在根配置中更改了它。但它被重定向到/ Acccount/Login页面并抛出404页面,因为我没有这样的控制器。 – Prasad 2011-05-17 13:41:09

+0

这很奇怪,因为我刚刚在这个时候对POC进行了验证,并且它按照预期的方式工作,重定向到web.conf(根目录)中指定的操作... – 2011-05-17 13:46:40

+0

您可以发布客户授权属性代码来交叉检查我是否缺少什么? – Prasad 2011-05-17 13:49:38