Solved

Store UserID on Authentication

Posted on 2007-04-10
2
477 Views
Last Modified: 2012-05-05
Hi

I am trying to implement membership authentication in .NET using the login control. I must say the simplest thing is defeating me. I want to be able to authenticate then store the userID for later reference. If I do not set my onauthenticate then the button event defaults to a ValidateUser function and I can authenticate fine but I can't capture the ID. So I have wrapped the ValidateUser function in a sub that is called by the onauthentication setting. That enables me to grab my ID but then my validation fails. My code is below, can some kind soul please point my in the right direction???

cheers
Verdy

<%@ Page Language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim result As Guid
    Dim userUID As Guid
   
    Private Sub Authentication(ByVal sender As System.Object, ByVal e As AuthenticateEventArgs)
        userUID = ValidateUser(Login1.UserName, Login1.Password)
        Session("UserUID") = result.ToString()
    End Sub
       
    Private Function ValidateUser(ByVal userName As String, ByVal passWord As String) As Guid
        If ((Login1.UserName Is Nothing)) Then
            System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
            Return 0
        End If
        If ((Login1.UserName.Length = 0) Or (Login1.UserName.Length > 30)) Then
            System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
            Return 0
        End If

        If (Login1.Password Is Nothing) Then
            System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
            Return 0
        End If
        If ((Login1.Password.Length = 0) Or (Login1.Password.Length > 25)) Then
            System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
            Return 0
        End If

        Try
        conn = New SqlConnection("server=.\SQLEXPRESS;Integrated Security=SSPI;database=EpiData")
        conn.Open()

        cmd = New SqlCommand("Select UserId from vw_aspnet_MembershipUsers where UserName=@userName and Password=@Password", conn)
        cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 256)
        cmd.Parameters("@UserName").Value = Login1.UserName
        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 256)
        cmd.Parameters("@Password").Value = Login1.Password

        result = cmd.ExecuteScalar()
       
        cmd.Dispose()
        conn.Dispose()

        End Try

        If result Is Nothing Then
            Return 0
        Else
            Return result
        End If
           
    End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
   <form id="NCACCHLogin" runat="server">

    <div >
    <h3>
   NCACCH Logon Page
</h3>
                                    
    </div>
        <asp:Login ID="Login1"
            DisplayRememberMe="false"  
            BorderColor="gray" BorderStyle="Ridge" BorderWidth="1px"  
            ForeColor="white"
            BackColor="blue"
            Onauthenticate="Authentication"
            RememberMeSet="false"
            runat="server">
        </asp:Login>
           </form>
</body>
</html>
0
Comment
Question by:verdante
2 Comments
 
LVL 3

Accepted Solution

by:
JipFromParis earned 500 total points
ID: 18880942
Replace your script at the beginning of the page with :

<script runat="server">
    Dim userUID As Guid
   
    Private Sub Authenticated(ByVal sender As Object, ByVal e As EventArgs)
        userUID = Membership.Providers(Login1.MembershipProvider).GetUser(Login1.UserName, True).ProviderUserKey
        Session("UserUID") = userUID.ToString()
    End Sub
</script>

Modify the Login1 control like that :

<asp:Login ID="Login1"
            DisplayRememberMe="false"  
            BorderColor="gray" BorderStyle="Ridge" BorderWidth="1px"  
            ForeColor="white"
            BackColor="blue"
            OnLoggedIn="Authenticated"
            RememberMeSet="false"
            runat="server">
        </asp:Login>    

You willl now handle the OnLoggedIn event and not anymore the Onauthenticate event. Hope this helps
0
 

Author Comment

by:verdante
ID: 18881387
Thx JfromP
Verdy
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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