Question

I am trying to grab the user name from my AuthCookie to use to control other variables. The cookie is defined in the index here:

<HttpPost()> _
Public Function Index(model As users.LogOnModel) As ActionResult
    'call LogOnModel
    '--------------------------
    If ModelState.IsValid Then

        'check username and password
        If model.pwd = db.users.First(Function(t) t.NT_id = model.NT_id).pwd Then

            'create an authentication cookie
            FormsAuthentication.SetAuthCookie(model.NT_id, False) 'set to false to destroy cookie on browser close

            'redirect action if login is successful
            Return RedirectToAction("Construction", "Home")
        Else
            ModelState.AddModelError("", "Invalid Username or Password")
        End If
    End If
    Return View(model)
End Function

I am trying to declare it in a variable here to be used to control other variables:

' User variables
    '---------------------------
    Public Shared uNT_id = IF(HttpContext.Current.User.Identity.Name is nothing, System.Environment.UserName, Trycast(HttpContext.Current.User.Identity.Name, String))


    Public Shared uid = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).app_user_id
    Public Shared ussn = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).ssn
    Public Shared upwd = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).pwd
    Public Shared uname_first = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).name_first

    ' App variables
    '---------------------------
    Public Shared appcount = db_apps.app_users.Count(Function(t) t.NT_id = uNT_id)
    Public Shared Function appid_multiple() As String
        Dim appidQ = From v In db_apps.app_users
                     Where v.NT_id = uNT_id
                     Select v

        'Create a String representation
        Dim stringList As New System.Text.StringBuilder
        Dim counter As Integer = 0
        For Each v In appidQ
            stringList.Append(v.app_id.ToString())

            'Separator
            If Not counter = appidQ.Count - 1 Then
                stringList.Append(",")
            End If
            counter += 1
        Next
        Return stringList.ToString
    End Function
    Public Shared appid = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).app_id

The end result is uNT_id needs to equal either the username they log into the website with, or the username they logged onto there computer with so for example mine should be:

uNT_id = "z148658"

and then I would use uNT_id to control all these other variables.

Was it helpful?

Solution

The following code returns the string I am trying to return

Public Shared uNT_id = If(HttpContext.Current.User.Identity.IsAuthenticated, HttpContext.Current.User.Identity.Name, System.Environment.UserName)

however if a user logs in and terminates the window, which SHOULD kill the cookie, the HttpContext.Current.User.Identity.Name is still equivalent to the last user to log in. The same holds true if a new user attempts to log in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top