我正在研究基於Parse.com框架的ASP.NET MVC5應用程序。從任務中檢索T <T>
既然不能使用解析登錄方法,我不得不使用張貼在這裏的方法來解決它的侷限性:Parse.com multiple users issue
這裏是我的登錄方法(只是微小的變化):
public async Task<ActionResult> Login(AccountModel model) //no returnUrl string
{
ParseUser user;
try
{
user = await ParseUser.LogInAsync(model.UserName, model.Password);//login parse user
}
catch (Exception e)
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
//making setAuthCookie get parse object id instead of username
FormsAuthentication.SetAuthCookie(user.ObjectId, model.RememberMe);
ParseUser.LogOut(); //log out parse user
return RedirectToAction("index", "home"); //Redirect to Action
}
所以基本上我(解析)登錄用戶,將AuthCookie設置爲它的對象ID,然後(解析)註銷用戶。這樣我可以有多個用戶登錄.Out SetAuthCookie我現在可以獲得用戶ID。
但是,我想顯示一些額外的用戶數據(如用戶地址,類型,名稱,姓氏)在parse.com雲。所以我想我會寫一個方法,通過使用當前通過身份驗證的用戶ID來獲取這些數據,用數據填充我的AccountModel類對象,然後將其傳遞給視圖。這是它會怎麼看起來像一個鬆散的想法(我知道語法可能是錯誤的,我沒有訪問我的Visual Studio現在):
的UserData模式:
public async Task<AccountModel> GetUserData()
{
AccountModel userData = new AccountModel();
ParseObject user;
ParseQuery<ParseObject> query = ParseObject.GetQuery("_User");
try
{
//i can't remember how to get authenticated user identity
user = await query.GetAsync(AuthenticatedUser.Identity());
}
catch(Exception e)
{
//code to handle exception
}
userData.Name = user.Get<string>("Name");
userData.Lastname = user.Get<string>("Lastname");
userData.Adress = user.Get<string>("Adress");
return userData; //it will probably throw an error
}
控制器:
public ActionResult Index()
{
UserData model = new UserData();
return View(model.GetUserData());
}
所以現在它可能會引發錯誤(不能從任務< T>返回T),我不知道如何解決這一問題,這樣我就可以在用戶數據得到當前登錄。
我在我的網站上有用戶名和姓氏顯示的導航欄,所以我必須在每次顯示頁面時以某種方式獲取當前登錄的用戶數據。是否有任何解決方法/更簡單的方法來實現這一目標?