C# 伊森罗问题
我是第一次使用表单身份验证,我正在使用一个来自web的示例来学习,我将其包含在我的web.config中C# 伊森罗问题,c#,asp.net,C#,Asp.net,我是第一次使用表单身份验证,我正在使用一个来自web的示例来学习,我将其包含在我的web.config中 <authentication mode="Forms"> <forms name="MYWEBAPP.ASPXAUTH" loginUrl="Login.aspx" protection="All" path="/"/> </authentication> <authorization>
<authentication mode="Forms">
<forms name="MYWEBAPP.ASPXAUTH" loginUrl="Login.aspx" protection="All" path="/"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
我还在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);
}
但是发生了一些奇怪的事情,因为它只写“accounters”(注意,“accounters”是分隔逗号字符串中的firts元素),而不写应该显示的其他角色。我更改了btnlogin click事件中角色列表的顺序,将“copiers”作为第一个元素写入,并且在页面中仅写入“copiers”
我尝试了不同的组合,并且总是打印分隔逗号字符串的第一个元素
对不起,我不知道这里发生了什么,所有的角色都在那里吗?正常吗?还是我忘了什么
提前感谢。请将空格插入
"accountants, seekers, copiers, typers"
请尝试在逗号后不加空格: 会计、求职者、复印机、打字机
拆分将创建诸如“会计”、“搜索者”、“复印机”、“打字机”等字符串,您要拆分的“,”。。。但当您初始化角色字符串时,它实际上是“,”(逗号空间) 这方面的一个技巧是使用调试器并使用即时窗口实际“查看”正在发生的事情
"accountants, seekers, copiers, typers"