Solved

Store UserID on Authentication

Posted on 2007-04-10
2
474 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
Comment Utility
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
Comment Utility
Thx JfromP
Verdy
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now