首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IsInRole问题

IsInRole问题
EN

Stack Overflow用户
提问于 2008-11-17 19:10:44
回答 3查看 1.9K关注 0票数 2

我是第一次使用表单身份验证,我使用了一个来自web的示例来学习,我将其包含在我的web.config中

代码语言:javascript
复制
    <authentication mode="Forms">
        <forms name="MYWEBAPP.ASPXAUTH" loginUrl="Login.aspx" protection="All" path="/"/>
    </authentication>
    <authorization>
        <deny users="?"/>
    </authorization>

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

代码语言:javascript
复制
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编写了代码;

代码语言:javascript
复制
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);
            }
        }
    }
}

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

代码语言:javascript
复制
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);
}

但是发生了一些奇怪的事情,导致它只写了"accountants“(请注意,"accountants”是逗号分隔字符串中的第一个元素),而不是应该显示的其他角色。我更改了btnlogin click事件中角色列表的顺序,将“only”作为第一个元素,并且在页面中只写入了“only”。

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

很抱歉我的无知,但是这里发生了什么,所有的角色都在那里吗?正常吗?还是我忘了什么?

提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-11-17 19:14:51

将空格放入

代码语言:javascript
复制
"accountants, seekers, copiers, typers"
票数 2
EN

Stack Overflow用户

发布于 2008-11-17 19:16:46

试着去掉逗号后面的空格:“会计员,搜索者,复印机,打字员”

拆分将会产生像“会计”,“搜索者”,“复印机”,“打字员”,

票数 2
EN

Stack Overflow用户

发布于 2008-11-17 19:17:20

你在分裂“,”……但是当你初始化你的角色字符串时,它实际上是",“(逗号空格)。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/296538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档