我试图从我的AuthCookie中获取用户名来控制其他变量。 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
我试图在一个变量中声明它用于控制其他变量:
' 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
最终结果是uNT_id需要等于他们登录网站的用户名,或者他们登录计算机的用户名,例如我的应该是:
uNT_id =“z148658”
然后我会使用uNT_id来控制所有这些其他变量。
答案 0 :(得分:0)
以下代码返回我尝试返回的字符串
Public Shared uNT_id = If(HttpContext.Current.User.Identity.IsAuthenticated, HttpContext.Current.User.Identity.Name, System.Environment.UserName)
但是如果用户登录并终止窗口,该窗口应该杀死cookie,则HttpContext.Current.User.Identity.Name仍然等同于最后一个登录用户。如果新用户尝试,则同样适用登录。