【问题标题】:Unauthenticated user redirection for a custom membership system mvc5自定义会员系统 mvc5 的未经身份验证的用户重定向
【发布时间】:2015-12-25 07:39:09
【问题描述】:

我正在使用 mvc5 开发一个 erp 系统。我有自己的会员系统,一旦用户登录,用户和他的用户权限就会保存在会话变量中。我想授权部分几乎完成了。但是对于身份验证,我现在试图限制未经身份验证的用户的访问。我是这样试的

在我的 web.config 中

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

在控制器上我放置了 [Authorize] 属性。

这适用于未经身份验证的用户。他们被重定向到登录页面。但这也限制了经过身份验证的用户的访问。(这意味着未经身份验证和经过身份验证的用户都不能访问具有授权属性的控制器)。

如果我删除授权属性,它会给我一个错误,因为没有用户可以分配给会话变量。这意味着没有授权属性 即使 web.config 已被修改,未经身份验证的用户也可以访问控制器。

我认为这涉及创建自定义属性。但我不知道如何解决这个问题。所有帮助表示赞赏。提前致谢!

【问题讨论】:

    标签: security authentication asp.net-mvc-5 authorization


    【解决方案1】:

    经过研究,我想出了解决方案。 我创建了一个自定义授权属性。

    public class CustomAuthorize : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var authroized = base.AuthorizeCore(httpContext);
    
            // Now check the session:
            var myvar = httpContext.Session["User"];
            if (myvar == null)
            {
                // the session has expired
                return false;
            }
    
            return true;
        }
    }
    

    在我的控制器上而不是 [authorize] 我放了 [CustomAuthorize]。

    在 web.config 中

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

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 2020-02-22
      • 2019-10-15
      • 2014-03-08
      • 1970-01-01
      • 2019-04-25
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多