无法使用 Context.User.IsRole 获取角色
我编写以下代码来访问该页面,但这对我不起作用
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将角色字符串作为“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.
这是因为您尚未配置
RoleManager
。在您的 web.Config 中它被禁用。您需要使用现有的 RoleProvider 或编写自己的 RoleProvider。
示例配置
以下是一些有关角色提供程序的链接
RoleProvider
实现自定义角色提供程序
That is because you have not configured the
RoleManager
. In your web.Config it is disabled.You need to use an exisiting RoleProvider or write your own.
Sample configuration
Here are some links about role providers
RoleProvider
Implementing Custom Role Provider