2016-08-01 22 views
0

Identity Frameworkを使用してASP.Netアプリケーションを作成しました。ユーザ認証に成功した後、System.Web.HttpContext.Current.User.Identitynullで、System.Web.HttpContext.Current.User.Identity.IsAuthenticatedfalseログイン認証後、ユーザーはNullです

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 
     var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 
       UserManager.AddClaim(User.Identity.GetUserId(),new Claim(ClaimTypes.Role,"Admin")); 

である私は、認証後に認証されたユーザに対して、いくつかの役割を追加する必要があります。

+0

最初のリダイレクト後にユーザーIDを取得できます – anand

答えて

4

最初にリダイレクトの後にUser.Identity.GetUserId()の値が得られます。リダイレクトするとすぐにCookieが作成されたら、User.Identity.GetUserId()またはUser.Identity.IsAuthenticatedのいずれかを使用します。

+0

どの場所(機能/クラス)で認証されたユーザーに役割を割り当てることができますか? – Sami

+0

@Sami:なぜログイン後に役割を割り当てるのですか?それはDbに格納する必要があります。 –

関連する問題