【问题标题】:WebSecurity.GetUserID(HttpContext.Current.User.Identity.Name) does not always return the UserIDWebSecurity.GetUserID(HttpContext.Current.User.Identity.Name) 并不总是返回 UserID
【发布时间】:2016-07-06 18:58:41
【问题描述】:

我有一个WebMatrix 应用程序,我正在使用WebSecurity.GetUserId(Http.Current.User.Identity.Name) 来获取UserID。有时它返回UserId,有时它返回-1。因此,我有一个登录页面,它应该获取登录到应用程序的人的UserId

这是我的代码:

    var UserId = WebSecurity.GetUserId(HttpContext.Current.User.Identity.Name);
    var User = WebSecurity.CurrentUserName;
    var UserEmail = WebSecurity.CurrentUserName;
    var dbApp = Database.Open("ApplicationServices"); 
    var selectUser = "SELECT UserId, UserName, Location FROM LocationTable, UserTable WHERE (UserId) = (@0) AND (UserLocation) = (Id)";  
    var person = dbApp.QuerySingle(selectUser, UserId);  

为什么不一致?

【问题讨论】:

    标签: sql-server security razor webmatrix


    【解决方案1】:

    您(或您的用户)可能只是遇到session timeout(即 WebSecurity.CurrentUserId = -1),在这种情况下,您希望在用户超时后将其重定向回您的登录页面.您需要确定您使用的是表单身份验证、Windows 身份验证还是(ick)服务器会话,以确定/设置超时值,并根据需要进行测试。

    对您的用户来说最好的办法是使用AuthorizeAttribute/AuthorizeCore 和它的 HandleUnauthorizedRequest 方法在会话到期时将他们重定向回您的登录页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多