[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Asp.net Login control customize error message

Posted on 2010-11-09
8
Medium Priority
?
1,129 Views
Last Modified: 2012-05-10
I am trying to customize the login control so when the user enters a bad username or password it will show a message in the label below, the problem is that this is not firing up, and instead the page is refreshed (when incorrect login info is entered and I see  asp.net "

message Error authenticating user. Logon failure: unknown user name or bad password"

How can I show a pop up java message or at least tell my label to show the error?

I am using the following event but doesnt seem to work? any ideaS?
Protected Sub Login1_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoginError

        'There was a problem logging in the user


        'Display the failure message in a client-side alert box
        ClientScript.RegisterStartupScript(Me.GetType(), "LoginError", _
        String.Format("alert('{0}');", Login1.FailureText.Replace("'", "\'")), True)



        'See if this user exists in the database
        Dim userInfo As MembershipUser = Membership.GetUser(Login1.UserName)

        If userInfo Is Nothing Then
            'The user entered an invalid username...
            lblLoginErrorDetails.Text = "There is no user in the database with the username " & Login1.UserName
        Else
            'See if the user is locked out or not approved
            If Not userInfo.IsApproved Then
                lblLoginErrorDetails.Text = "Your account has not yet been approved by the site's administrators. Please try again later..."
            ElseIf userInfo.IsLockedOut Then
                lblLoginErrorDetails.Text = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked."
            Else
                'The password was incorrect (don't show anything, the Login control already describes the problem)
                lblLoginErrorDetails.Text = String.Empty
            End If
        End If


        ''Display the failure message in a client-side alert box
        'ClientScript.RegisterStartupScript(Me.GetType(), "LoginError", _
        'String.Format("alert('{0}');", Login1.FailureText.Replace("'", "\'")), True)

    End Sub

Open in new window

0
Comment
Question by:TonyReba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 34094689
what is the code executed prior to that?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34094887
is a function to authenticate
Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal pwd As String) As Boolean

        Dim domainAndUsername As String = domain & "\" & username
        Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)

        Try
            'Bind to the native AdsObject to force authentication.			
            Dim obj As Object = entry.NativeObject
            Dim search As DirectorySearcher = New DirectorySearcher(entry)

            search.Filter = "(SAMAccountName=" & username & ")"
            search.PropertiesToLoad.Add("cn")
            Dim result As SearchResult = search.FindOne()

            If (result Is Nothing) Then
                Return False
            End If

            'Update the new path to the user in the directory.
            _path = result.Path
            _filterAttribute = CType(result.Properties("cn")(0), String)

        Catch ex As Exception
            Throw New Exception("Error authenticating user. " & ex.Message)
        End Try

        Return True
    End Function

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34094920
Ok you are getting this message:

 Error authenticating user. Logon failure: unknown user name or bad password"

That is from this catch-block:

Catch ex As Exception
            Throw New Exception("Error authenticating user. " & ex.Message)
        End Try

So you will never hit the Login_Error code.

Plus, since you are trying to authenticate against AD, what you are doing inside LoginError handler doesn't make sense because in there you are trying to use built-in memebership provider.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:TonyReba
ID: 34095036
So how can I solve this, and have a customize error message in the login form?

Do I remove the try catch block?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34095053
can I just redirect here?

 If (result Is Nothing) Then
                Return False
            End If
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 34095102
So how can I solve this, and have a customize error message in the login form?
--> I am not sure how you would trap those errors with custom AD authentication

Do I remove the try catch block?
--> Modify catch block like this and see if it helps.
      Catch ex As Exception
           Return False
        End Try
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34095127
Can I only refresh the login page when the ad username or password doesnt match?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34095142
upss I am an idiot,  that worked,,

Throw New Exception("Error authenticating user. " & ex.Message)

was messing everything!!



0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question