IsInRole问题

时间:2008-11-17 19:10:44

标签: c# asp.net

我第一次使用表单身份验证,我正在使用Web上的一个示例来学习,我包含在我的web.config中

    <authentication mode="Forms">
        <forms name="MYWEBAPP.ASPXAUTH" loginUrl="Login.aspx" protection="All" path="/"/>
    </authentication>
    <authorization>
        <deny users="?"/>
    </authorization>

然后我创建了一个用于登录“login.aspx”的页面,并在一个按钮上编码,只是为了开始;

private void btnLogin_Click(Object sender, EventArgs e)
    {
        // Initialize FormsAuthentication
        FormsAuthentication.Initialize();

         // Create a new ticket used for authentication
         FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1, // Ticket version
            Username.Value, // Username associated with ticket
            DateTime.Now, // Date/time issued
            DateTime.Now.AddMinutes(30), // Date/time to expire
            true, // "true" for a persistent user cookie
            "accountants, seekers, copiers, typers", // User-data, in this case the roles
            FormsAuthentication.FormsCookiePath);// Path cookie valid for

         // Encrypt the cookie using the machine key for secure transport

         string hash = FormsAuthentication.Encrypt(ticket);
         HttpCookie cookie = new HttpCookie(
            FormsAuthentication.FormsCookieName, // Name of auth cookie
            hash); // Hashed ticket

         // Set the cookie's expiration time to the tickets expiration time
         if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

         // Add the cookie to the list for outgoing response
         Response.Cookies.Add(cookie);
    }

我也用Global.asax编码;

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    if(HttpContext.Current.User != null)
    {
        if(HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (HttpContext.Current.User.Identity is FormsIdentity)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;

                // Get the stored user-data, in this case, our roles
                string userData = ticket.UserData;
                string[] roles = userData.Split(',');
                HttpContext.Current.User = new GenericPrincipal(id, roles);
            }
        }
    }
}

最后在另一页中,我试图确认所获得的角色;

protected void Page_Load(object sender, EventArgs e)
{
    string str = null;

    if (User.IsInRole("seekers"))
    {
        str += " seekers ";
    }

    if (User.IsInRole("accountants"))
    {
        str += " accountants ";
    }

    if (User.IsInRole("copiers"))
    {
        str += "copiers";
    }

    Response.Write(str);
}

但是发生了一些奇怪的事情,因为它只会写“会计师”(请注意“会计师”是分隔逗号字符串中的第一个元素),而不是应该显示的其他角色。我在btnlogin点击事件中更改了角色列表的顺序,将“复印机”作为第一个元素,并且在页面中只写了“复印机”。

我尝试过不同的组合,并且始终打印出分隔逗号字符串的第一个元素。

对不起我的无知,但这里发生了什么,是否所有的角色?是正常的?还是我忘记了什么?

提前致谢。

3 个答案:

答案 0 :(得分:2)

删除

中的空格
"accountants, seekers, copiers, typers"

答案 1 :(得分:2)

在逗号后面没有空格的情况下尝试:     “会计师,求职者,复印机,typers”

斯普利特将创建像“会计师”,“寻求者”,“复印机”,“打字员”这样的字符串,

答案 2 :(得分:1)

你正在分裂','...但是当你初始化你的角色时,它实际上是“,”(逗号空间)。

这方面的一个提示是使用调试器并使用即时窗口实际“看到”正在发生的事情。