Friday, March 9, 2012

Login with different Roles

Hello readers

I have a problem with the following:

I made a login page in Visual Web Developer 2005 and I used the ASP.net Configationtool in the Website menu. Made some couple test accounts to login and made a role: "Beheerders". Made 1 of my test accounts a "Administrators".

So my problem is when I log in with the account with the "Beheerders" role, i go to the page of the "normal user". I tried to look in many sites, I could find some nice aid but not 100% the one that could solve my problem.

This is my code i programmed in the seperatefile of Login.aspx

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports Login

PartialClass Login

Inherits System.Web.UI.Page

ProtectedSub Login1_LoggedIn(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Login1.LoggedIn

Dim userinfoAs MembershipUser = Membership.GetUser(Login1.UserName)

Dim UserRol()AsString

UserRol = Roles.GetRolesForUser(userinfo.UserName)

If (Roles.IsUserInRole("Beheerders") =False)Then

Response.Redirect("~\Index.aspx")

EndIf

EndSub

ProtectedSub Login1_LoggedIn()

EndSub

ProtectedSub Login1_LoginError(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Login1.LoginError

'Parameters instellen voor InvalidCredentialsLogDataSource

InvalidCredentialsLogDataSource.InsertParameters("ApplicationName").DefaultValue = Membership.ApplicationName

InvalidCredentialsLogDataSource.InsertParameters("UserName").DefaultValue = Login1.UserName

InvalidCredentialsLogDataSource.InsertParameters("IPAddress").DefaultValue = Request.UserHostAddress

InvalidCredentialsLogDataSource.InsertParameters("Password").DefaultValue = Login1.Password

'Er was een probleem bij het aanmelden

'Nakijken of gebruiker bestaat in de databank

Dim userInfoAs MembershipUser = Membership.GetUser(Login1.UserName)

If userInfoIsNothingThen

'Ongeldige gebruikersnaam...

LoginErrorDetails.Text ="Geen gebruiker met naam """ & Login1.UserName &""" in de databank !"

Else

'Nakijken of gebruiker Lockedout of Approved is

IfNot userInfo.IsApprovedThen

LoginErrorDetails.Text ="Uw account is nog niet goedgekeurd!"

ElseIf userInfo.IsLockedOutThen

LoginErrorDetails.Text ="Uw account is geblokkeerd wegens te veel mislukte aanmeldpogingen!"

Else

'Het wachtwoord was verkeerd

LoginErrorDetails.Text ="Verkeerd wachtwoord - """ & Login1.Password &""""

EndIf

EndIf

'Record wegschrijven naar datasource

InvalidCredentialsLogDataSource.Insert()

EndSub

EndClass

Thanks for your help.

Regardings

Griffin1987

I believe your problem here is that in the loggedIn event, the cookie is not actually set yet. To get the name of the user, use the name from the control which I think is

login1.UserName

No comments:

Post a Comment