If red.Read() Then Dim tc As FormsAuthenticationTicket
tc = New FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student")
Dim encrypt As String = FormsAuthentication.Encrypt(tc)
Dim ck As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encrypt)
ck.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(ck)
Dim ru As String = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName, False)
Response.Redirect(ru) Label1.Text = User.Identity.Name & User.IsInRole("student")
Else
Label1.Text = "error1"
End If
问题是我看别人的资料是FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student") 里面的"student"是角色
可我在做的时候 User.IsInRole 是False 但是 User.Identity.Name 的植 又可以取到
该怎么坚决啊
tc = New FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student")
Dim encrypt As String = FormsAuthentication.Encrypt(tc)
Dim ck As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encrypt)
ck.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(ck)
Dim ru As String = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName, False)
Response.Redirect(ru) Label1.Text = User.Identity.Name & User.IsInRole("student")
Else
Label1.Text = "error1"
End If
问题是我看别人的资料是FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student") 里面的"student"是角色
可我在做的时候 User.IsInRole 是False 但是 User.Identity.Name 的植 又可以取到
该怎么坚决啊
解决方案 »
- gridview列中模板列里的detailview引用父项gridview里的某行某列?在线急等,高手救命!!
- iis 站点 为什么只能放访问首页?
- 使用验证控件时页面无法显示
- 急!联机摄像头自动获取头像图片上传到服务器怎么实现啊???
- 一个asp.net调用存储过程的问题,取不到数据,搞不定,郁闷
- foreach (DataRowView dr in dv) 为什么"DataRowView",显示不出来???
- 如何把aaaa"vvvv"nnnnnn字符串中的"vvvv"替换掉?(s.replace('vvvv',"")不行,不知怎么处理双引号
- 求一个正则表达式的写法
- 23种设计模式另理解-转帖-不知道那里来的,希望对各位有帮助
- 能不能用vs2005来写asp.net1.1的代码?
- 为什么我的User.IsInRole("student")总是False 在线等(特菜的问题 坚决马上给分)
- 关于SQL查询问题
了, Label1.Text = User.Identity.Name & User.IsInRole("student")
这句话根本就不会执行。。把验证票放在CooKie,还要通过
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{
return;
}
string[] roles = authTicket.UserData.Split(new char[]{','});//如果存取多个角色,我们把它分解
FormsIdentity id = new FormsIdentity( authTicket );
GenericPrincipal principal = new GenericPrincipal(id, roles);
Context.User =principal;//存到HttpContext.User中
}
把角色取出来
http://www.hubro.net/html_OS-Gdo9vh3Q.ashx
我估计那时1.X版本用的
我看别的写的都 引进了空间 可我在写的时候2.0里根本无法引进空间
<%@ imports System.Security.Principal%>
<%@ Imports System.Web.Security %>
Principal不是Application的有效属性 大家帮帮忙 提供点VB的好吗
这也抱错了
现在公布坚决方法但不是最佳的 因为在不加TRY的时候 等出会报错
其他的不变 主要是Global.asax 的内容要改 我改了两个 随便那一个都可以 特别感谢啊亮Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
If Not (HttpContext.Current.User Is DBNull.Value) Then
Try
If (HttpContext.Current.User.Identity.IsAuthenticated) Then
If (HttpContext.Current.User.Identity.AuthenticationType = "Forms") Then
Dim fi As FormsIdentity = HttpContext.Current.User.Identity
Dim ticket As FormsAuthenticationTicket = fi.Ticket
Dim userData As String = ticket.UserData
Dim roles As String() = userData.Split(",")
HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(fi, roles)
End If
End If
Catch ex As Exception
Return
End Try
End If
End Sub或者
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim cookieName As String = FormsAuthentication.FormsCookieName
Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
Dim authTicket As FormsAuthenticationTicket
Try
authTicket = FormsAuthentication.Decrypt(authCookie.Value)
Catch ex As Exception
Return
End Try
Dim roles As String() = authTicket.UserData.Split("'")
Dim id As FormsIdentity = New FormsIdentity(authTicket)
Dim principal As System.Security.Principal.GenericPrincipal = New System.Security.Principal.GenericPrincipal(id, Roles)
Context.User = principal
End Sub