当用户未登录时,MVC中的授权属性不会重定向

时间:2016-05-11 23:58:11

标签: c# asp.net-mvc authentication

我有自己的身份验证,在Global.asax.cs中看起来像这样:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie == null || authCookie.Value == "")
        return;

    FormsAuthenticationTicket authTicket;

    try
    {
        authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    }
    catch
    {
        return;
    }

    string[] roles = authTicket.UserData.Split(';');

    if (Context.User != null)
        Context.User = new GenericPrincipal(Context.User.Identity, roles);
}

然后在Web.config中:

<authentication mode="Forms">
   <forms loginUrl="~/Users/Login" defaultUrl="~/Items/Index" timeout="432000" />
</authentication>

它可以授权罚款。我通过访问页面测试了它,没有登录和写入: HttpContext.Current.User.Identity.IsAuthenticated 这将返回false。手动进入〜/ Users / Login并登录将导致它返回true。因此,身份验证有效,它不会重定向未登录到〜/ Items / Index页面的用户。我在控制器上有[Authorize]属性:

[Authorize]
public class ItemsController : Controller
{

有关问题的建议吗?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。事实证明,NinjectWebCommon文件是引起它的文件。我只是删除它,它再次工作。

相关问题