hye..I am following aspnet tutorial_vb until tutorial 10. But I want to redirect user after login into a specific web page.
For example, when admin login by enter username and password, he will be directed into admin page while supervisor and user will directed into their page. I have created folder roles contain managesRoles and userAndRoles for admin and supervisor. But I don't
know how to redirect into a specific page after they login. Cn anyone help me please..
Else
' Username/password are not valid...
e.Authenticated = False
End If
End Sub
Protected Sub myLogin_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles myLogin.LoginError
' Determine why the user could not login...
myLogin.FailureText = "Your login attempt was not successful. Please try again."
' Does there exist a User account for this user?
Dim usrInfo As MembershipUser = Membership.GetUser(myLogin.UserName)
If usrInfo IsNot Nothing Then
' Is this user locked out?
If usrInfo.IsLockedOut Then
myLogin.FailureText = "Your account has been locked out because of too many invalid login attempts. Please contact the administrator to have your account unlocked."
ElseIf Not usrInfo.IsApproved Then
myLogin.FailureText = "Your account has not yet been approved. You cannot login until an administrator has approved your account."
End If
End If
End Sub
End Class
This is my code for login.aspx.vb. I don't know where to put the role checking. I want admin will be directed into admin page after login, supervisor into supervisor page and users into user page. Cn you help me...
Protected Sub myLogin_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles myLogin.Authenticate
If Membership.ValidateUser(myLogin.UserName, myLogin.Password) Then
' Username/password are valid
e.Authenticated = True
// Here you need to check the uiser role and based on it wirte the cases for each role and redirect the user to the corresponding page.
Response.Redirect("Saya.aspx")
Else
' Username/password are not valid...
e.Authenticated = False
End If
// Here you need to check the uiser role and based on it wirte the cases for each role and redirect the user to the corresponding page.
Response.Redirect("Saya.aspx")
Cn you explain how I cn I use response.redirect(admin.aspx) for admin after login and same goes for supervisor response.redirect(supervisor.aspx) and users response.redirect(user.aspx) in the login page?
Below is my code for manageRoles.aspx.vb
Partial Class Roles_ManageRoles
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DisplayRolesInGrid()
End If
End Sub
Private Sub DisplayRolesInGrid()
RoleList.DataSource = Roles.GetAllRoles()
RoleList.DataBind()
End Sub
Protected Sub CreateRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateRoleButton.Click
Dim newRoleName As String = RoleName.Text.Trim()
If Not Roles.RoleExists(newRoleName) Then
' Create the role
Roles.CreateRole(newRoleName)
' Refresh the RoleList Grid
DisplayRolesInGrid()
End If
RoleName.Text = String.Empty
End Sub
Protected Sub RoleList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles RoleList.RowDeleting
' Get the RoleNameLabel
Dim RoleNameLabel As Label = CType(RoleList.Rows(e.RowIndex).FindControl("RoleNameLabel"), Label)
' Delete the role
Roles.DeleteRole(RoleNameLabel.Text, False)
' Rebind the data to the RoleList grid
DisplayRolesInGrid()
End Sub
End Class
And code below is usersandroles.aspx.vb which is located in roles folder
Partial Class Roles_UsersAndRoles
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' Bind the users and roles
BindUsersToUserList()
BindRolesToList()
' Check the selected user's roles
CheckRolesForSelectedUser()
'Display those users belonging to the currently selected role
DisplayUsersBelongingToRole()
End If
End Sub
Private Sub BindRolesToList()
' Get all of the roles
Dim roleNames() As String = Roles.GetAllRoles()
UsersRoleList.DataSource = roleNames
UsersRoleList.DataBind()
RoleList.DataSource = roleNames
RoleList.DataBind()
End Sub
#Region "'By User' Interface-Specific Methods"
Private Sub BindUsersToUserList()
' Get all of the user accounts
Dim users As MembershipUserCollection = Membership.GetAllUsers()
UserList.DataSource = users
UserList.DataBind()
End Sub
Protected Sub UserList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles UserList.SelectedIndexChanged
CheckRolesForSelectedUser()
End Sub
Private Sub CheckRolesForSelectedUser()
' Determine what roles the selected user belongs to
Dim selectedUserName As String = UserList.SelectedValue
Dim selectedUsersRoles() As String = Roles.GetRolesForUser(selectedUserName)
' Loop through the Repeater's Items and check or uncheck the checkbox as needed
For Each ri As RepeaterItem In UsersRoleList.Items
' Programmatically reference the CheckBox
Dim RoleCheckBox As CheckBox = CType(ri.FindControl("RoleCheckBox"), CheckBox)
' See if RoleCheckBox.Text is in selectedUsersRoles
If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then
RoleCheckBox.Checked = True
Else
RoleCheckBox.Checked = False
End If
Next
End Sub
Protected Sub RoleCheckBox_CheckChanged(ByVal sender As Object, ByVal e As EventArgs)
' Reference the CheckBox that raised this event
Dim RoleCheckBox As CheckBox = CType(sender, CheckBox)
' Get the currently selected user and role
Dim selectedUserName As String = UserList.SelectedValue
Dim roleName As String = RoleCheckBox.Text
' Determine if we need to add or remove the user from this role
If RoleCheckBox.Checked Then
' Add the user to the role
Roles.AddUserToRole(selectedUserName, roleName)
' Display a status message
ActionStatus.Text = String.Format("User {0} was added to role {1}.", selectedUserName, roleName)
Else
' Remove the user from the role
Roles.RemoveUserFromRole(selectedUserName, roleName)
' Display a status message
ActionStatus.Text = String.Format("User {0} was removed from role {1}.", selectedUserName, roleName)
End If
' Refresh the "by role" interface
DisplayUsersBelongingToRole()
End Sub
#End Region
#Region "'By Role' Interface-Specific Methods"
Protected Sub RoleList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RoleList.SelectedIndexChanged
DisplayUsersBelongingToRole()
End Sub
Private Sub DisplayUsersBelongingToRole()
' Get the selected role
Dim selectedRoleName As String = RoleList.SelectedValue
' Get the list of usernames that belong to the role
Dim usersBelongingToRole() As String = Roles.GetUsersInRole(selectedRoleName)
' Bind the list of users to the GridView
RolesUserList.DataSource = usersBelongingToRole
RolesUserList.DataBind()
End Sub
Protected Sub RolesUserList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles RolesUserList.RowDeleting
' Get the selected role
Dim selectedRoleName As String = RoleList.SelectedValue
' Reference the UserNameLabel
Dim UserNameLabel As Label = CType(RolesUserList.Rows(e.RowIndex).FindControl("UserNameLabel"), Label)
' Remove the user from the role
Roles.RemoveUserFromRole(UserNameLabel.Text, selectedRoleName)
' Refresh the GridView
DisplayUsersBelongingToRole()
' Display a status message
ActionStatus.Text = String.Format("User {0} was removed from role {1}.", UserNameLabel.Text, selectedRoleName)
' Refresh the "by user" interface
CheckRolesForSelectedUser()
End Sub
Protected Sub AddUserToRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddUserToRoleButton.Click
' Get the selected role and username
Dim selectedRoleName As String = RoleList.SelectedValue
Dim userToAddToRole As String = UserNameToAddToRole.Text
' Make sure that a value was entered
If userToAddToRole.Trim().Length = 0 Then
ActionStatus.Text = "You must enter a username in the textbox."
Exit Sub
End If
' Make sure that the user exists in the system
Dim userInfo As MembershipUser = Membership.GetUser(userToAddToRole)
If userInfo Is Nothing Then
ActionStatus.Text = String.Format("The user {0} does not exist in the system.", userNameToAddToRole)
Exit Sub
End If
' Make sure that the user doesn't already belong to this role
If Roles.IsUserInRole(userToAddToRole, selectedRoleName) Then
ActionStatus.Text = String.Format("User {0} already is a member of role {1}.", UserNameToAddToRole, selectedRoleName)
Exit Sub
End If
' If we reach here, we need to add the user to the role
Roles.AddUserToRole(userToAddToRole, selectedRoleName)
' Clear out the TextBox
userNameToAddToRole.Text = String.Empty
' Refresh the GridView
DisplayUsersBelongingToRole()
' Display a status message
ActionStatus.Text = String.Format("User {0} was added to role {1}.", UserNameToAddToRole, selectedRoleName)
' Refresh the "by user" interface
CheckRolesForSelectedUser()
End Sub
#End Region
End Class
I'm a bit blur how to redirect into a specific page based on roles assign after login. I would very appreciate if you cn help to solve this problems.
I have got the answer. Thanks for those who help me in this forums and also my lect Mr Tan. The first thing is, I must check role in login so that from login part it will be direct into a specific page based on authorization.
hanis
Member
44 Points
35 Posts
redirect user to a specific page based on roles after login
Mar 01, 2012 02:46 AM|LINK
hye..I am following aspnet tutorial_vb until tutorial 10. But I want to redirect user after login into a specific web page.
For example, when admin login by enter username and password, he will be directed into admin page while supervisor and user will directed into their page. I have created folder roles contain managesRoles and userAndRoles for admin and supervisor. But I don't know how to redirect into a specific page after they login. Cn anyone help me please..
ncsubbu
Member
535 Points
150 Posts
Re: redirect user to a specific page based on roles after login
Mar 01, 2012 03:12 AM|LINK
Hi
After role checking in the login page ,based on the role of user redirect to particular pages using response.redirect() or server.transfer();
http://subbarao515.wordpress.com
hanis
Member
44 Points
35 Posts
Re: redirect user to a specific page based on roles after login
Mar 01, 2012 03:49 AM|LINK
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub myLogin_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles myLogin.Authenticate
If Membership.ValidateUser(myLogin.UserName, myLogin.Password) Then
' Username/password are valid
e.Authenticated = True
Response.Redirect("Saya.aspx")
Else
' Username/password are not valid...
e.Authenticated = False
End If
End Sub
Protected Sub myLogin_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles myLogin.LoginError
' Determine why the user could not login...
myLogin.FailureText = "Your login attempt was not successful. Please try again."
' Does there exist a User account for this user?
Dim usrInfo As MembershipUser = Membership.GetUser(myLogin.UserName)
If usrInfo IsNot Nothing Then
' Is this user locked out?
If usrInfo.IsLockedOut Then
myLogin.FailureText = "Your account has been locked out because of too many invalid login attempts. Please contact the administrator to have your account unlocked."
ElseIf Not usrInfo.IsApproved Then
myLogin.FailureText = "Your account has not yet been approved. You cannot login until an administrator has approved your account."
End If
End If
End Sub
End Class
This is my code for login.aspx.vb. I don't know where to put the role checking. I want admin will be directed into admin page after login, supervisor into supervisor page and users into user page. Cn you help me...
daisydain
Member
222 Points
51 Posts
Re: redirect user to a specific page based on roles after login
Mar 01, 2012 04:21 AM|LINK
Hello,
Try this
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub myLogin_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles myLogin.Authenticate
If Membership.ValidateUser(myLogin.UserName, myLogin.Password) Then
' Username/password are valid
e.Authenticated = True
// Here you need to check the uiser role and based on it wirte the cases for each role and redirect the user to the corresponding page.
Response.Redirect("Saya.aspx")
Else
' Username/password are not valid...
e.Authenticated = False
End If
End Sub
hanis
Member
44 Points
35 Posts
Re: redirect user to a specific page based on roles after login
Mar 01, 2012 04:39 AM|LINK
Hello,
// Here you need to check the uiser role and based on it wirte the cases for each role and redirect the user to the corresponding page.
Response.Redirect("Saya.aspx")
Cn you explain how I cn I use response.redirect(admin.aspx) for admin after login and same goes for supervisor response.redirect(supervisor.aspx) and users response.redirect(user.aspx) in the login page?
Below is my code for manageRoles.aspx.vb
Partial Class Roles_ManageRoles
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DisplayRolesInGrid()
End If
End Sub
Private Sub DisplayRolesInGrid()
RoleList.DataSource = Roles.GetAllRoles()
RoleList.DataBind()
End Sub
Protected Sub CreateRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateRoleButton.Click
Dim newRoleName As String = RoleName.Text.Trim()
If Not Roles.RoleExists(newRoleName) Then
' Create the role
Roles.CreateRole(newRoleName)
' Refresh the RoleList Grid
DisplayRolesInGrid()
End If
RoleName.Text = String.Empty
End Sub
Protected Sub RoleList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles RoleList.RowDeleting
' Get the RoleNameLabel
Dim RoleNameLabel As Label = CType(RoleList.Rows(e.RowIndex).FindControl("RoleNameLabel"), Label)
' Delete the role
Roles.DeleteRole(RoleNameLabel.Text, False)
' Rebind the data to the RoleList grid
DisplayRolesInGrid()
End Sub
End Class
And code below is usersandroles.aspx.vb which is located in roles folder
Partial Class Roles_UsersAndRoles
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' Bind the users and roles
BindUsersToUserList()
BindRolesToList()
' Check the selected user's roles
CheckRolesForSelectedUser()
'Display those users belonging to the currently selected role
DisplayUsersBelongingToRole()
End If
End Sub
Private Sub BindRolesToList()
' Get all of the roles
Dim roleNames() As String = Roles.GetAllRoles()
UsersRoleList.DataSource = roleNames
UsersRoleList.DataBind()
RoleList.DataSource = roleNames
RoleList.DataBind()
End Sub
#Region "'By User' Interface-Specific Methods"
Private Sub BindUsersToUserList()
' Get all of the user accounts
Dim users As MembershipUserCollection = Membership.GetAllUsers()
UserList.DataSource = users
UserList.DataBind()
End Sub
Protected Sub UserList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles UserList.SelectedIndexChanged
CheckRolesForSelectedUser()
End Sub
Private Sub CheckRolesForSelectedUser()
' Determine what roles the selected user belongs to
Dim selectedUserName As String = UserList.SelectedValue
Dim selectedUsersRoles() As String = Roles.GetRolesForUser(selectedUserName)
' Loop through the Repeater's Items and check or uncheck the checkbox as needed
For Each ri As RepeaterItem In UsersRoleList.Items
' Programmatically reference the CheckBox
Dim RoleCheckBox As CheckBox = CType(ri.FindControl("RoleCheckBox"), CheckBox)
' See if RoleCheckBox.Text is in selectedUsersRoles
If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then
RoleCheckBox.Checked = True
Else
RoleCheckBox.Checked = False
End If
Next
End Sub
Protected Sub RoleCheckBox_CheckChanged(ByVal sender As Object, ByVal e As EventArgs)
' Reference the CheckBox that raised this event
Dim RoleCheckBox As CheckBox = CType(sender, CheckBox)
' Get the currently selected user and role
Dim selectedUserName As String = UserList.SelectedValue
Dim roleName As String = RoleCheckBox.Text
' Determine if we need to add or remove the user from this role
If RoleCheckBox.Checked Then
' Add the user to the role
Roles.AddUserToRole(selectedUserName, roleName)
' Display a status message
ActionStatus.Text = String.Format("User {0} was added to role {1}.", selectedUserName, roleName)
Else
' Remove the user from the role
Roles.RemoveUserFromRole(selectedUserName, roleName)
' Display a status message
ActionStatus.Text = String.Format("User {0} was removed from role {1}.", selectedUserName, roleName)
End If
' Refresh the "by role" interface
DisplayUsersBelongingToRole()
End Sub
#End Region
#Region "'By Role' Interface-Specific Methods"
Protected Sub RoleList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RoleList.SelectedIndexChanged
DisplayUsersBelongingToRole()
End Sub
Private Sub DisplayUsersBelongingToRole()
' Get the selected role
Dim selectedRoleName As String = RoleList.SelectedValue
' Get the list of usernames that belong to the role
Dim usersBelongingToRole() As String = Roles.GetUsersInRole(selectedRoleName)
' Bind the list of users to the GridView
RolesUserList.DataSource = usersBelongingToRole
RolesUserList.DataBind()
End Sub
Protected Sub RolesUserList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles RolesUserList.RowDeleting
' Get the selected role
Dim selectedRoleName As String = RoleList.SelectedValue
' Reference the UserNameLabel
Dim UserNameLabel As Label = CType(RolesUserList.Rows(e.RowIndex).FindControl("UserNameLabel"), Label)
' Remove the user from the role
Roles.RemoveUserFromRole(UserNameLabel.Text, selectedRoleName)
' Refresh the GridView
DisplayUsersBelongingToRole()
' Display a status message
ActionStatus.Text = String.Format("User {0} was removed from role {1}.", UserNameLabel.Text, selectedRoleName)
' Refresh the "by user" interface
CheckRolesForSelectedUser()
End Sub
Protected Sub AddUserToRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddUserToRoleButton.Click
' Get the selected role and username
Dim selectedRoleName As String = RoleList.SelectedValue
Dim userToAddToRole As String = UserNameToAddToRole.Text
' Make sure that a value was entered
If userToAddToRole.Trim().Length = 0 Then
ActionStatus.Text = "You must enter a username in the textbox."
Exit Sub
End If
' Make sure that the user exists in the system
Dim userInfo As MembershipUser = Membership.GetUser(userToAddToRole)
If userInfo Is Nothing Then
ActionStatus.Text = String.Format("The user {0} does not exist in the system.", userNameToAddToRole)
Exit Sub
End If
' Make sure that the user doesn't already belong to this role
If Roles.IsUserInRole(userToAddToRole, selectedRoleName) Then
ActionStatus.Text = String.Format("User {0} already is a member of role {1}.", UserNameToAddToRole, selectedRoleName)
Exit Sub
End If
' If we reach here, we need to add the user to the role
Roles.AddUserToRole(userToAddToRole, selectedRoleName)
' Clear out the TextBox
userNameToAddToRole.Text = String.Empty
' Refresh the GridView
DisplayUsersBelongingToRole()
' Display a status message
ActionStatus.Text = String.Format("User {0} was added to role {1}.", UserNameToAddToRole, selectedRoleName)
' Refresh the "by user" interface
CheckRolesForSelectedUser()
End Sub
#End Region
End Class
I'm a bit blur how to redirect into a specific page based on roles assign after login. I would very appreciate if you cn help to solve this problems.
hanis
Member
44 Points
35 Posts
Re: redirect user to a specific page based on roles after login
Mar 03, 2012 11:19 AM|LINK
Anyone expert here...cn you help me..
hanis
Member
44 Points
35 Posts
Re: redirect user to a specific page based on roles after login
Mar 08, 2012 12:14 AM|LINK
I have got the answer. Thanks for those who help me in this forums and also my lect Mr Tan. The first thing is, I must check role in login so that from login part it will be direct into a specific page based on authorization.