简体   繁体   English

将FormsAuthentication cookie转换为字符串以在公共共享变量中使用

[英]Converting FormsAuthentication cookie to string for use in public shared variable

I am trying to grab the user name from my AuthCookie to use to control other variables. 我试图从AuthCookie中获取用户名,以用于控制其他变量。 The cookie is defined in the index here: Cookie在此处的索引中定义:

<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需要等于他们登录网站所使用的用户名,或者与其登录到那里的计算机所使用的用户名相等,例如我的用户名应为:

uNT_id = "z148658" uNT_id =“ z148658”

and then I would use uNT_id to control all these other variables. 然后我将使用uNT_id来控制所有其他变量。

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. 但是,如果用户登录并终止要杀死Cookie的窗口,则HttpContext.Current.User.Identity.Name仍然等同于最后登录的用户。如果新用户尝试登录,则同样适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM