Solved

Store UserID on Authentication

Posted on 2007-04-10
2
476 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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