asp.net,角色未通过表单身份验证分配

发布于 2024-11-05 15:29:46 字数 1033 浏览 0 评论 0原文

又一天遇到有关 ASP.NET 角色的问题。所以我目前正在尝试为我的网站设置角色。我使用这段代码来制作 formsAuthentication 票证。

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, //Ticket version
                p.firstName, //username
                DateTime.Now, 
                DateTime.Now.AddMinutes(30), 
                false, //true for persistant user cookie
                "Admin",
                FormsAuthentication.FormsCookiePath);
            string hashCookies = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
            Response.Cookies.Add(cookie);
            Response.Redirect("Default.aspx");

您可以看到我正在向此人添加“admin”角色。 p。是一个对象,名字是一个字符串。在下一段代码中,我检查该人在进入管理站点时是否获得了管理员角色。如果他不是管理员,则必须将其退回。

if (User.IsInRole("Admin") != true)
     {
         Response.Redirect("Default.aspx");
     }

奇怪的是它让我返回默认页面?有人知道答案吗?

附言。我知道您必须在 web.config 文件的不同位置设置授权。但如果他连这个角色都拿不到,那现在也没关系。

Another day with problems concerning roles in asp.net. So I'm currently trying to set up roles for my website. I use this piece of code for making the formsAuthentication ticket.

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, //Ticket version
                p.firstName, //username
                DateTime.Now, 
                DateTime.Now.AddMinutes(30), 
                false, //true for persistant user cookie
                "Admin",
                FormsAuthentication.FormsCookiePath);
            string hashCookies = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
            Response.Cookies.Add(cookie);
            Response.Redirect("Default.aspx");

You can see that I'm adding the role "admin" to this person. p. is an object and firstname is a string. In the next piece of code I check wether the person got the admin role when he enters an admin site. if he's not an admin he has to be returned.

if (User.IsInRole("Admin") != true)
     {
         Response.Redirect("Default.aspx");
     }

the odd thing is that it returns me to the default page? anyone knows the answer?

PS. I know you have to set up authorization in the different locations in the web.config file. But if he doesn't even get the role it doesn't matter for now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2024-11-12 15:29:46

我不确定你能否让角色以这种方式工作。它的设计工作方式是通过角色管理器。有关详细信息,请参阅本文

您可能需要查看的一件事是 cacheRolesInCookie 设置。如果禁用该功能,您可以确保默认的表单身份验证内容不会从身份验证 cookie 重新加载角色。

I'm not sure you'll be able to get roles to work this way. The way it was designed to work is through the role manager. See this article for maore details.

One thing you may want to look at is the cacheRolesInCookie setting. If that is disabled, you can be sure that the default forms authentication stuff will not reload the roles from the authentication cookie.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文