We help IT Professionals succeed at work.
Get Started

ASP.Net Identity and Accessing currently logged in user (ASP.Net 4.6

Last Modified: 2015-09-15
I have begun using ASPNet Ientity in ASP.Net 4.6. There are some differences when attempting to get the currently logged in user. The following code get the logged in user and assigns to a textbox called UserId. It assigns the UserName to the Textbox instead of UserId but that is fine for now.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (User.Identity.IsAuthenticated) Then

            UserId.Text = User.Identity.Name
        End If

    End Sub

My problem is the old Member Provider code I was using before to insert the currently logged in User with the other records. I do not know why this will not work anymore but I am sure that it is not the correct syntax to work with the new ASP.Net Identity Membership Provider.

Dim MemUser As MembershipUser
        MemUser = Membership.GetUser()
        UserId.Text = MemUser.ProviderUserKey.ToString()

I do not know how but I feel the Syntax is incorrect now. I am hoping someone can tell me the correct way to do this with the new ASPNet Identity. I know a lot of folks are not using this version yet but I hope someone can help. The entire code behind page is below. Everything works except I am not able to submit the currently logged in user. Again, I am able to assign the logged in user to a textbox. I am just missing the other components.

Imports System.Data.SqlClient
Imports Microsoft.AspNet.Identity

Public Class member1001glf1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (User.Identity.IsAuthenticated) Then

            UserId.Text = User.Identity.Name
        End If

    End Sub

    Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.
        If (FileUpload1.HasFile) Then
            'Check the file size
            If (FileUpload1.PostedFile.ContentLength < 88100000) Then
                'Call a helper method routine to save the file.
                'Notify the User
                UploadStatusLabel.Text = "File exceeds limit of 8 MB"
            End If
            ' Notify the user that a file was Not uploaded.
            UploadStatusLabel.Text = "You did not specify a file To upload."
        End If
    End Sub
    Sub SaveFile(ByVal file As HttpPostedFile)

        ' Specify the path to save the uploaded file to.
        Dim savePath As String = HttpContext.Current.Server.MapPath("memuploads\")

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
            Dim counter As Integer = 2
            While (System.IO.File.Exists(pathToCheck))
                ' If a file with this name already exists,
                ' prefix the filename with a number.
                tempfileName = counter.ToString() + fileName
                pathToCheck = savePath + tempfileName
                counter = counter + 1
            End While

            fileName = tempfileName

            ' Notify the user that the file name was changed.
            UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" +
                                     "Your file was saved as " + fileName


            ' Notify the user that the file was saved successfully.
            UploadStatusLabel.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.

        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser()
        UserId.Text = MemUser.ProviderUserKey.ToString()

        Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim strQuery As String = "insert into tblFiles (UserId, savedfor, performedby, searchcode, exercisename, bodypartmg, bodypartspecific, description, FileName, FilePath) values(@UserId, @savedfor, @performedby, @searchcode, @exercisename, @bodypartmg, @bodypartspecific, @description, @FileName, @FilePath)"
        Dim cmd As New SqlCommand(strQuery)
        cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = MemUser.ProviderUserKey.ToString()
        cmd.Parameters.Add("@SavedFor", SqlDbType.VarChar).Value = savedfor.Text
        cmd.Parameters.Add("@performedby", SqlDbType.VarChar).Value = performedby.Text
        cmd.Parameters.Add("@searchcode", SqlDbType.VarChar).Value = searchcode.Text
        cmd.Parameters.Add("@exercisename", SqlDbType.VarChar).Value = exercisename.Text
        cmd.Parameters.Add("@bodypartmg", SqlDbType.VarChar).Value = bodypartmg.Text
        cmd.Parameters.Add("@bodypartspecific", SqlDbType.VarChar).Value = bodypartspecific.Text
        cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = description.Text
        cmd.Parameters.AddWithValue("@FileName", fileName)
        cmd.Parameters.AddWithValue("@FilePath", "memuploads\" & fileName)
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Catch ex As Exception
        End Try

    End Sub

End Class
Watch Question
This problem has been solved!
Unlock 2 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE