2015-04-26 31 views
0

我使用bellow登錄方法在MVC 5應用程序中登錄。 我想要做的是獲取當前登錄的用戶的ID。 我試圖使用這2種方法:使用WebMetrix從MVC獲取UserId

// 1

//這一個產生一個異常:

Guid loggedUser = (Guid)Membership.GetUser().ProviderUserKey; 

其他信息:未設置爲一個 的實例對象引用目的。

// 2

//設置loggedUser變量控制器內空

loggedUser = User.Identity.GetUserId(); 

//登錄方法

public ActionResult Login(Login logindata, string ReturnUrl) 
        { 
        if (ModelState.IsValid) 
         { 
         if (WebSecurity.Login(logindata.Username, logindata.Password)) 
          { 
          if (ReturnUrl != null) 
           { 
           return Redirect(ReturnUrl); 
           } 
          return RedirectToAction("Index", "Home"); 
          } 
         } 
        ModelState.AddModelError("", "Sorry the username or password is invalid "); 
        return View(logindata); 
        } 

我的問題是什麼,我做錯了,如果有可能獲得像這樣登錄的用戶ID?

感謝

+0

我也試過這樣在各自的視圖:@ HttpContext.Current.User.Identity.GetUserId()返回null ... –

回答

0

我用這個方法和工作:

WebSecurity.GetUserId("loggedUser") 
相關問題