Solved

Store UserID on Authentication

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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