Solved

Store UserID on Authentication

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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 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