Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc FormsAuthenticationTicket用户数据在加密后仍然可读(带有forms Auth的asp.net MVC3。)_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc FormsAuthenticationTicket用户数据在加密后仍然可读(带有forms Auth的asp.net MVC3。)

Asp.net mvc FormsAuthenticationTicket用户数据在加密后仍然可读(带有forms Auth的asp.net MVC3。),asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我正在用新的ASP.NET MVC3框架和FormsAuth构建一个网站。以确保网站的安全。我将用户角色存储在FormsAuthenticationTicket的UserData属性中(手动设置cookie),然后在将票据添加到cookie之前调用票据上的encrypt方法(请参见下面的标准票据剪) 现在,我创建了一个自定义属性,可以检查用户是否为1。和2。具有管理员角色(来自票据)。(下文) 当在具有属性注释的类中发生操作时,将调用此派生类的AuthorizeCore方法 protected

我正在用新的ASP.NET MVC3框架和FormsAuth构建一个网站。以确保网站的安全。我将用户角色存储在FormsAuthenticationTicket的UserData属性中(手动设置cookie),然后在将票据添加到cookie之前调用票据上的encrypt方法(请参见下面的标准票据剪)

现在,我创建了一个自定义属性,可以检查用户是否为1。和2。具有管理员角色(来自票据)。(下文) 当在具有属性注释的类中发生操作时,将调用此派生类的AuthorizeCore方法

protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            IPrincipal user = httpContext.User;
            if (!user.Identity.IsAuthenticated)
            {
                return false;
            }
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = httpContext.Request.Cookies[cookieName];
            if (authCookie == null)
                return false;

            FormsAuthenticationTicket authTicket =       FormsAuthentication.Decrypt(authCookie.Value);
            if (authTicket.UserData != UserType.Administrator.ToString())
                return false;
            return true;
这就是我感到困惑的地方

当我跟踪正在执行的代码(在调试中使用有效凭据)并检查每行上的变量值时,encryptedTicket会在将其添加到reponseccookie之前进行加密

但是,当我在调用控制器(索引页的)时检查AuthorizeCore方法时,其获取的参数HttpContext包含所有未加密的票证,因此在读取cookie时不再需要解密票证

为什么我看到票据成功地在登录控制器中被加密,并将其发送回客户端,但当我在authorizedAdministrator类中接收到httpcontext时,它又全部未加密

很抱歉问了这么长的问题/故事,可能有一个简单而简短的答案。 希望我的故事是清楚的


谢谢。

Forms auth需要在页面处理管道的早期解密cookie,以确定用户是否已获得授权——这是它填写user.Identity等详细信息的时候。

谢谢,我们希望这样做,但希望确定:)为什么不使用用户角色来存储角色,而不是自己编写并将其放入UserData字段?
protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            IPrincipal user = httpContext.User;
            if (!user.Identity.IsAuthenticated)
            {
                return false;
            }
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = httpContext.Request.Cookies[cookieName];
            if (authCookie == null)
                return false;

            FormsAuthenticationTicket authTicket =       FormsAuthentication.Decrypt(authCookie.Value);
            if (authTicket.UserData != UserType.Administrator.ToString())
                return false;
            return true;