2017-07-31 87 views
-1

我們正在開發一個mvc項目,我們已經做了登錄註冊頁面。但我們沒有使用asp.net身份框架。我們正在從我們的數據庫中檢查用戶數據,這是在sql server studio中,並在用戶註冊時粘貼用戶數據。在不使用asp.net標識的情況下獲取記錄的用戶標識

在我們的項目中,這是一種導師配對的遊戲,導師給他/她的導師一個項目。當我們創建一個項目時,我們使用數據庫中的兩個表,一個是Mentee表,另一個是Mentor表。

在mentee表中創建新行很容易,但是當記錄的用戶創建新任務時,我們需要他/她的id在mentor表中創建一行。因爲如果您已登錄並創建任務,則您是導師,我們需要從Users表中獲取已記錄的用戶標識。

我希望我能讓自己變得非常清楚,我們需要您的幫助。如果您需要我們項目的任何代碼,我可以編輯帖子。下面的代碼

[HttpPost] 
public ActionResult NewLesson(DersAtamaModel model) 
{ 
    using (DENEME_V2Entities db = new DENEME_V2Entities()) 
    { 
     LESSON lesson = new LESSON(); 
     lesson.LName = model.Project_Name; 
     lesson.Intro = model.Intro; 
     db.LESSONS.Add(lesson); 
     db.SaveChanges(); 
     int Lesson_id = db.LESSONS.Where(x => x.LessonID == lesson.LessonID).FirstOrDefault().LessonID; 
     TaskCreateModel tcmodel = new TaskCreateModel(); 
     tcmodel.Lesson_ID = Lesson_id; 
     MENTEE mentee = new MENTEE(); 
     mentee.LessonID = Lesson_id; 
     mentee.UserID = model.secilenKullanıcıId; 
     mentee.Intro = "Intro"; 
     mentee.Statu = 0; 
     db.MENTEEs.Add(mentee); 
     db.SaveChanges(); 
     //MENTOR mentor = new MENTOR(); 
     string userId = User.Identity.GetUserId(); 
      return RedirectToAction("CreateTasks", "Lesson",tcmodel); 
    } 

} 
+0

您使用登錄時是否在會話中存儲了UserId? –

+0

你能解釋我們如何在會話 –

回答

0

使用存儲在會話登錄ID,當需要的時候使用日誌,然後retrive細節:

[HttpPost] 
public ActionResult Login(UserInfo user) 
{ 
    Session["UserId"] = user.Id; 
} 

[HttpPost] 
public ActionResult NewLesson(DersAtamaModel model) 
{ 
    int userId = Convert.ToInt32(Session["UserId"]); 
} 

我只提供的代碼來設置和獲取會話。當用戶註銷時,您應該清除會話使用Session.Abondon()Session["UserId"] = null

+0

中存儲用戶標識!有效!!!!!! –

相關問題