【问题标题】:Unable to get the Role using Context.User.IsRole无法使用 Context.User.IsRole 获取角色
【发布时间】:2011-07-12 09:40:47
【问题描述】:

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

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>

但是即使他通过了身份验证,我也无法获得该角色,任何人都可以告诉我该怎么做

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

【问题讨论】:

  • 为什么是roleManager enabled="false" ???
  • 因为我是从我的 sql server 获取角色

标签: asp.net c#-3.0 roles


【解决方案1】:

您正在将角色字符串作为“UserData”添加到FormsAuthenticationTicket。没有魔法可以将此值推断给角色。

我建议你再次查阅文档。

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

请参阅http://www.codeproject.com/KB/web-security/formsroleauth.aspx 了解不带 RoleManager 的示例。

【讨论】:

  • 嗨 Leppie 我没听懂你能告诉我应该在我的代码中做哪些更改
  • 查看您传递角色名称的参数。它不是用于角色,而是用于用户数据。
  • 即使我从我的代码中删除它并且尝试过的问题仍然是一样的
  • 这完全在意料之中。 IIRC,如果不使用内置 RoleManager,您需要将 FormsIdentity 包装到 GenericPricipal 并将其分配给 Context.User
【解决方案2】:

那是因为你没有配置RoleManager。在您的 web.Config 中它被禁用。

<roleManager enabled="false" />

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

示例配置

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

这里有一些关于角色提供者的链接

RoleProvider

Implementing Custom Role Provider

【讨论】:

  • 我没有使用默认角色,我使用的是数据中的用户定义角色
  • Context.User 的类型为 RolePrinciple。当您调用IsInRole 方法时,它使用配置的RoleProvider。所以你需要编写一个自定义角色提供者来使用该方法。
猜你喜欢
  • 2020-12-17
  • 2015-07-17
  • 2021-10-22
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 2013-05-21
  • 1970-01-01
相关资源
最近更新 更多