2017-01-03 7 views
0

私のAsp.Net MVCアプリケーションでは、ユーザーは自分のUserIdを含むURLを持つ別のシステムから来ています。ユーザの初期要求では、認証プロセスの後、ユーザの言語オプションは、対応するクッキー値に同期される。Asp.Net MVCカルチャーのCookieは、UI言語ではなく2番目の更新まで変更されます

[HttpPost] 
    [AllowAnonymous] 
    public JsonResult Login(Guid userId, UserType userType) 
    { 
     try 
     { 
      IAccount account = null; 

      if (userType == UserType.SystemUser) 
       account = _accountService.CheckSystemUser(userId); 
      if (userType == UserType.Employee) 
       account = _accountService.CheckEmployee(userId); 

      if (account == null) throw new Exception(ErrorMessages.UserNotFound); 

      var roles = account.Roles.ToArray(); 

      var principalModel = new CustomPrincipalViewModel 
      { 
       UserId = account.UserId.ToString(), 
       FullName = account.FullName, 
       Roles = roles, 
       Language = account.Language 
      }; 

      var userData = JsonConvert.SerializeObject(principalModel); 
      var ticket = new FormsAuthenticationTicket(1, principalModel.FullName, DateTime.Now, 
       DateTime.Now.AddMinutes(30), false, userData); 
      var encryptedTicket = FormsAuthentication.Encrypt(ticket); 
      var requestCookie =Request.Cookies[FormsAuthentication.FormsCookieName]; 
      if (requestCookie != null) requestCookie.Expires = DateTime.Now.AddDays(-1); 
      var responseCookie =Response.Cookies[FormsAuthentication.FormsCookieName]; 
      if(responseCookie != null) responseCookie.Expires = DateTime.Now.AddDays(-1); 
      var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
      Response.Cookies.Add(cookie); 

      SetCulture(account.Language.CultureCode); 

      return Json(new {isSuccess = true, userFullName = account.FullName}); 

     } 
     catch (BusinessExceptions.CannotConnectToCrmServiceException ex) 
     { 
      ElmahManager.Log(ex); 
      return Json(new { isSuccess = false, errorText = ErrorMessages.GeneralError }); 
     } 
     catch (Exception exception) 
     { 
      ElmahManager.Log(exception); 
      return Json(new { isSuccess = false, errorText = ErrorMessages.GeneralError }); 
     } 
    } 

    private void SetCulture(string culture) 
    { 
     culture = CultureHelper.GetImplementedCulture(culture); 
     var requestCookie =Request.Cookies["_culture"]; 
     if (requestCookie != null) requestCookie.Expires = DateTime.Now.AddDays(-1); 


     var cultureCookie = Request.Cookies["_culture"]; 
     if (cultureCookie == null) 
     { 
      cultureCookie = new HttpCookie("_culture") 
      { 
       Value = culture, 
       Expires = DateTime.Now.AddYears(1) 
      }; 
     } 
     else 
     { 
      cultureCookie.Value = culture; 
      cultureCookie.Expires = DateTime.Now.AddYears(1); 
     } 
     Response.Cookies.Add(cultureCookie); 
     Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture); 
     Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 
    } 

このプロセスの後、データベースからユーザーの言語選択を変更してページを更新すると、ページ言語は変更されません。その間、Cookieの値をチェックすると、現在の言語の値と同じですが、ページが2度目にリフレッシュされるまでページ言語は変更されません。

+0

ログイン()ユーザーがデフォルト言語を使用した後、リフレッシュ後に適切な言語を取得できることを意味しますか? –

+0

なぜ変わるのですか?ログインメソッドの結果はjson(たぶんAJAXによってトリガーされます)です - カルチャークッキーを設定しているLoginメソッドを呼び出すことによってページがリフレッシュされることはありません。 –

+0

あなたは正しい@MaxMokrousov –

答えて

0

あなたは

public ActionResult Login(Guid userId, UserType userType) 

public JsonResult Login(Guid userId, UserType userType) 

を変更して、表示またはリダイレクトを返すことがあります。

[HttpPost] 
[AllowAnonymous] 
public ActionResult Login(Guid userId, UserType userType) 
{ 
    try{ 

    //Your logic 
    return View(new {isSuccess = true, userFullName = account.FullName}); 
    } 
    catch (BusinessExceptions.CannotConnectToCrmServiceException ex) 
    { 
     ElmahManager.Log(ex); 
     return View(new { isSuccess = false, errorText = ErrorMessages.GeneralError }); 
    } 
    catch (Exception exception) 
    { 
     ElmahManager.Log(exception); 
     return View(new { isSuccess = false, errorText = ErrorMessages.GeneralError }); 
    } 
} 
+0

私の前のコードはあなたが言ったことでした。しかし、顧客は、アプリケーションが初めて開かれたときにユーザーがログインするのを待つ時間が長すぎ、画面が空白になっていると述べました。その後、自分のコードを変更して、コードがすぐにビューに戻り、他のすべての操作をajaxクエリで行うようにしました。どのようにして、ユーザーを読み込み画面に置いて、同時に新しい言語のページを読み込む方法でこれを行いますか? –

+0

@Yusuf Duyar私はむしろ、ajaxを作るよりパフォーマンスの問題を調査するようお勧めします。しかし、あなたがlocation.reload()を呼び出さなければならないよりも、ajaxリクエストをしたい場合は、コールバックで。 –

+0

あなたの説明に感謝します:)私は認証プロセスを変更し、このアクションからビューを返します。 –

関連する問題