har problem med min form-auth den fattar inte vilken behörighet som användaren som loggar in har samt vilken användare som loggat in skrivs ej ut! Programarkivet:Exempel på formsauthentication mot SQL och AD är ett fullständigt exempel på forms authentication ... form-auth problem
Det verkar som om att oavsätt vilken behörighet man e skrivs administrator ut! i response.write,
Har i db "tabell användare" som innehåller: anvNamn, lösenord och anvgrupp.
global.asax
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
If Not HttpContext.Current.User Is Nothing Then
If HttpContext.Current.User.Identity.IsAuthenticated Then
If TypeOf HttpContext.Current.User.Identity Is FormsIdentity Then
Dim id As System.Web.Security.FormsIdentity = HttpContext.Current.User.Identity, FormsIdentity()
Dim ticket As FormsAuthenticationTicket = id.Ticket
Dim MyRoles(2) As String
MyRoles(0) = "admin"
MyRoles(1) = "user"
HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(id, MyRoles)
End If
End If
End If
Login.ascx
Sub Login_Click(ByVal Src As Object, ByVal E As EventArgs)
If Page.IsValid Then
FormsAuthentication.Initialize()
Dim ticket As FormsAuthenticationTicket
Dim strcookie As String
Dim cookie As HttpCookie
Dim strConn As String = ConfigurationSettings.AppSettings("Connstr").Replace("%MAPPATH%", Server.MapPath(" "))
Dim Conn As New OleDbConnection(strConn)
Conn.Open()
Dim strSQL As String = "SELECT losen FROM anvandare WHERE anvnamn = '" & txtanv.Text & "'"
Dim Cmd As New OleDbCommand(strSQL, Conn)
Dim Dr As OleDbDataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If Dr.Read() Then
ticket = New FormsAuthenticationTicket(1, txtanv.Text, DateTime.Now(), _
DateTime.Now.AddMinutes(30), spara.Checked)
strcookie = FormsAuthentication.Encrypt(ticket)
cookie = New HttpCookie(FormsAuthentication.FormsCookieName(), strcookie)
If (spara.Checked) Then cookie.Expires = ticket.Expiration
cookie.Path = FormsAuthentication.FormsCookiePath()
Response.Cookies.Add(cookie)
Dim strRedirect As String
strRedirect = Request("ReturnURL")
If strRedirect <> "" Then
Response.Redirect(strRedirect, True)
Else
strRedirect = "default.aspx"
Response.Redirect(strRedirect, True)
End If
Else
Response.Redirect("logon.aspx", True)
End If
End If
End Sub
loggaut.ascx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Me.IsPostBack Then
lbluser.Text = HttpContext.Current.User.Identity.Name
End If
End Sub
Sub Logout_Click(ByVal Src As Object, ByVal E As EventArgs)
FormsAuthentication.SignOut()
Server.Transfer("default.aspx")
End Sub
var e felet?
ps.såg att jag inte sparar vilken grupp personen tillhör i cookin:n men hur gör jag d?
Sv: form-auth problem