关于Forms验证中User.IsInRole()求教

atester 2003-12-12 05:36:12
我想用User.IsInRole()方法。在通过FormsAuthenticationTicket后,我不知道怎么向User里添加role。
我现在是这样做的:
string [] role = ticket.UserData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(User.Identity,role);
但是:
debug的时候发现这个User的IsAuthenticated为false。在其它页面上User的role还是没有。感觉虽然重新构造了User,但后续的使用中,并没有接纳它,只能使用User.Identity.Name
...全文
62 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
atester 2003-12-15
  • 打赏
  • 举报
回复
怎么还是没人帮忙回答啊?
uno 2003-12-15
  • 打赏
  • 举报
回复
只有利用多余的属性自己实现了
uno 2003-12-15
  • 打赏
  • 举报
回复
Forms验证模式下系统好像没有提供Role机制
  • 打赏
  • 举报
回复

if (reader.Read())
{ FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,Username.Value,DateTime.Now,DateTime.Now.AddMinutes(30), true, reader.GetString(0), // 用户数据(表中字段,包含个人角色:admin,user,etc)
FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

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

// Redirect to requested URL, or homepage if no previous page
// requested
string returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl == null) returnUrl = "/";

// Don't call FormsAuthentication.RedirectFromLoginPage since it
// could
// replace the authentication ticket (cookie) we just added
Response.Redirect(returnUrl);
}
else
{
// Never tell the user if just the username is password is incorrect.
// That just gives them a place to start, once they've found one or
// the other is correct!
ErrorLabel = "Username / password incorrect. Please try again.";
ErrorLabel.Visible = true;
}
  • 打赏
  • 举报
回复
正在研究中.......
look this:http://www.codeproject.com/aspnet/formsroleauth.asp
skylineforever 2003-12-13
  • 打赏
  • 举报
回复
帮你up
atester 2003-12-13
  • 打赏
  • 举报
回复
up
dub 2003-12-13
  • 打赏
  • 举报
回复
up

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧