0

我有這個以下MVC應用程序用戶沒有通過驗證,直到下一個頁面請求

問題是,當我試着去指定配置文件值:

  // Attempt to register the user 
      MembershipCreateStatus createStatus = MembershipService.CreateUser(model.Email, model.Password); 

      if (createStatus == MembershipCreateStatus.Success) 
      { 
       //Adding role 
       MembershipService.AddDefaultRole(model.Email); 

       FormsService.SignIn(model.Email, false /* createPersistentCookie */); 

       //Add other initial profile data 
       HttpContext.Profile["FirstName"] = model.FirstName; //PROBLEM 
       HttpContext.Profile["LastName"] = model.LastName; //PROBLEM 

       return RedirectToAction("List", new { area = "", controller = "Requests" }); 
      } 
      else 
      { 
       ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus)); 
      } 

內FormsService.SignIn(model.Email, false):

public void SignIn(string email, bool createPersistentCookie) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 

     FormsAuthentication.SetAuthCookie(email, createPersistentCookie); 
    } 

調用FormsAuthentication.SetAuthCookie後,用戶還沒有通過身份驗證? 我收到錯誤b.c.即時嘗試分配一些配置文件的值給匿名用戶。

任何想法?

回答

1

當您設置cookie時,它會添加到響應中,但IsAuthenticated bool是從請求中設置的。設置認證並設置會話變量後,您應該重定向到另一個頁面,如主頁或原始請求。

+0

這是有道理的。你知道在下一次請求之前更新配置文件值的簡便方法嗎?還是應該暫時存儲這些值並將其保存在下一頁的請求中?什麼是正確的做法? – Dhana

+0

你需要嗎?在您的初始化事件中,如果用戶已通過身份驗證但沒有配置文件信息,那麼我認爲應該加載它,但我從未使用過內置配置文件管理器,它不符合我的需要。 –

相關問題