0

How to display login failure text if user logging in user role id differ than vendors role ...

i m using the following code to authenticate user role during logging in

Protected Sub Login3_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login3.LoggingIn
        Dim user As TextBox = Me.Login3.FindControl("UserName")
        If Roles.IsUserInRole(user.Text, "Vendors") Then
            Login3.DestinationPageUrl = ("~/vendors/select_service.aspx")
        Else
            Login3.DestinationPageUrl = ("~/login.aspx")
        End If
    End Sub
prerna
  • 1
  • 3
  • The problem with this code was /// this will only validate the vendors role user to access teh default.aspx if i login with admin role credentials then it will not shows error ...but if i directly type ... default.aspx then this page will be accessed by vendors and admins .... but i want only vendor user role must access the page – prerna Apr 09 '11 at 09:20

2 Answers2

1

in my opinion, the roles purposes is not the login. login must validate credentials like username or password.

Roles in my opinion should used to determine if user have access or not to a specific section of your website/application.

to show the message, consider to add a label and change the label text property.

Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63
0

In my situation, I wanted to authenticate the users, but if they had no roles associated with their account, cancel the login and display a message. Based on Flavio's suggestion to use a Label, this is what I came up with. (LoginInput is my LoginControl)

Protected Sub LoginInput_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)

    Dim a As System.Web.UI.WebControls.Login = CType(sender, System.Web.UI.WebControls.Login)

    LoginError.Visible = False

    If (Membership.ValidateUser(a.UserName, a.Password)) Then
        If (Roles.GetRolesForUser(a.UserName).Count = 0) Then
            LoginError.Text = "Your account does not have permission to access the system."
            LoginError.Visible = True
            e.Cancel = True
        End If
    End If

End Sub
Walter Stabosz
  • 7,447
  • 5
  • 43
  • 75