无法使用 Context.User.IsRole 获取角色

发布于 2024-11-19 08:57:23 字数 1869 浏览 2 评论 0原文

我编写以下代码来访问该页面,但这对我不起作用

if (User.Identity.IsAuthenticated)
    {
        if (Context.User.IsInRole("DistrictAdmin"))
        {
            if (!IsPostBack)
            {
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    else
    {
        Response.Redirect("Default.aspx");
    } 

我的登录检查代码

  string RoleTypeID;
    objLogin.UserName = txtUsername.Text;
    objLogin.Password = txtPassword.Text;

    if (objLogin.getRoles(out RoleTypeID))
    {
        Session["RoleID"] = RoleTypeID;
        FormsAuthenticationTicket oAuthTicket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, RoleTypeID.ToString(), FormsAuthentication.FormsCookiePath);
        string encryptoAuthTicket = FormsAuthentication.Encrypt(oAuthTicket);


        HttpCookie oCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptoAuthTicket); // Name of auth cookie

        if (oAuthTicket.IsPersistent) oCookie.Expires = oAuthTicket.Expiration;

        HttpContext.Current.Response.Cookies.Add(oCookie);
        if (RoleTypeID == "DistrictAdmin" || RoleTypeID == "CampusAdministrator" || RoleTypeID == "LPACMember")
        {
            Response.Redirect("LEPstudentrecords.aspx");
        }
    }
    else
    {
        lblInvalid.Visible = true;
    }

和我的 web.config 我设置如下

     <roleManager enabled="false" />
    <authentication mode="Forms">
  <forms loginUrl="Default.aspx"
timeout="20" />
</authentication>

    <sessionState mode="InProc" timeout="180"></sessionState>

但即使他通过了身份验证,我也无法获得该角色谁能告诉我该怎么做

这是经过身份验证后的图像,一切都正常,但我无法访问该页面

输入图像此处描述

I write the following code to access the page but this is not working for me

if (User.Identity.IsAuthenticated)
    {
        if (Context.User.IsInRole("DistrictAdmin"))
        {
            if (!IsPostBack)
            {
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    else
    {
        Response.Redirect("Default.aspx");
    } 

My Login check code

  string RoleTypeID;
    objLogin.UserName = txtUsername.Text;
    objLogin.Password = txtPassword.Text;

    if (objLogin.getRoles(out RoleTypeID))
    {
        Session["RoleID"] = RoleTypeID;
        FormsAuthenticationTicket oAuthTicket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, RoleTypeID.ToString(), FormsAuthentication.FormsCookiePath);
        string encryptoAuthTicket = FormsAuthentication.Encrypt(oAuthTicket);


        HttpCookie oCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptoAuthTicket); // Name of auth cookie

        if (oAuthTicket.IsPersistent) oCookie.Expires = oAuthTicket.Expiration;

        HttpContext.Current.Response.Cookies.Add(oCookie);
        if (RoleTypeID == "DistrictAdmin" || RoleTypeID == "CampusAdministrator" || RoleTypeID == "LPACMember")
        {
            Response.Redirect("LEPstudentrecords.aspx");
        }
    }
    else
    {
        lblInvalid.Visible = true;
    }

and in my web.config i set as follows

     <roleManager enabled="false" />
    <authentication mode="Forms">
  <forms loginUrl="Default.aspx"
timeout="20" />
</authentication>

    <sessionState mode="InProc" timeout="180"></sessionState>

But i am unable to get the role even if he is authenticated can any one tell what to do

Here is the image after authenticated every thing is getting right but i am unable to access that page

enter image description here

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

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

发布评论

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

评论(2

简单 2024-11-26 08:57:23

您将角色字符串作为“UserData”添加到 FormsAuthenticationTicket 中。没有什么魔法可以将这个值推断给角色。

我建议你再次查阅文档。

http://msdn2.microsoft.com/en-us /library/system.web.security.formsauthenticationticket.formsauthenticationticket

请参阅http://www.codeproject.com/KB/web-security/formsroleauth.aspx 没有 RoleManager 的示例。

You are adding the role string as 'UserData' to the FormsAuthenticationTicket. There is no magic that will infer this value to a role.

I suggest you consult the documentation again.

http://msdn2.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.formsauthenticationticket

See http://www.codeproject.com/KB/web-security/formsroleauth.aspx for an example without RoleManager.

最丧也最甜 2024-11-26 08:57:23

这是因为您尚未配置RoleManager。在您的 web.Config 中它被禁用。

<roleManager enabled="false" />

您需要使用现有的 RoleProvider 或编写自己的 RoleProvider。

示例配置

<roleManager enabled="true" defaultProvider="SqlRoleManager">
  <providers>
    <add name="SqlRoleManager" 
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="SqlRoleManagerConnection"
         applicationName="MyApplication" />
  </providers>
</roleManager>

以下是一些有关角色提供程序的链接

RoleProvider

实现自定义角色提供程序

That is because you have not configured the RoleManager. In your web.Config it is disabled.

<roleManager enabled="false" />

You need to use an exisiting RoleProvider or write your own.

Sample configuration

<roleManager enabled="true" defaultProvider="SqlRoleManager">
  <providers>
    <add name="SqlRoleManager" 
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="SqlRoleManagerConnection"
         applicationName="MyApplication" />
  </providers>
</roleManager>

Here are some links about role providers

RoleProvider

Implementing Custom Role Provider

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