3

私はASP.NET EFエンドでコアIDを使用しています認証クッキーにユーザーに関連するデータを保存したいと思います。データをasp.netコアIDを持つクッキーに保存します

これは(appcontextを格納するためのデータである)私はASP.NET 4.6に行うために使用される方法です:

public static void IdentitySignin(AppContext appContext, string providerKey = null, bool isPersistent = false) 
{ 
    var claims = new List<Claim>(); 

    // create *required* claims 
    claims.Add(new Claim(ClaimTypes.NameIdentifier, appContext.UserId.ToString())); 
    claims.Add(new Claim(ClaimTypes.Name, appContext.UserName)); 

    // serialized AppUserState object 
    claims.Add(new Claim("appcontext" + EZA.Store.AppContext.Version, appContext.ToString())); 

    var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

    // add to user here! 
    AuthenticationManager.SignIn(new AuthenticationProperties() 
    { 
     AllowRefresh = true, 
     IsPersistent = isPersistent, 
     ExpiresUtc = DateTime.UtcNow.AddDays(7), 
    }, identity); 
} 

今はEFASP.NET Identityを使用していると私はいくつかのデータを格納するための方法を見つけることができませんクッキーに

+1

わかりません-to-aspnet-identity)? –

+0

あなたはそれを仲間に働かせましたか? – user3127109

答えて

6

AddClaimsAsyncまたはAddClaimAsyncUserManager<YourUserIdentity>としてください。このようなexempleのために、あなたのユーザーにサインインする:

public class AccountController : Controller 
{ 
    public UserManager<YourUserIdentity> UserManager { get; private set; } 

    public SignInManager<YourUserIdentity> SignInManager { get; private set; } 

    public AccountController(UserManager<YourUserIdentity> userManager, 
     SignInManager<YourUserIdentity> signInManager) 
    { 
     UserManager = userManager; 
     SignInManager = signInManager; 
    } 

    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = await UserManager.FindByNameAsync(model.UserName); 

      await UserManager.AddClaimAsync(user, new Claim("your-claim", "your-value")); 

      var signInStatus = await SignInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, lockoutOnFailure: false); 

      if (signInStatus.Succeeded) 
       return RedirectToLocal(returnUrl); 

      ModelState.AddModelError("", "Invalid username or password."); 
      return View(model); 
     } 

     // If we got this far, something failed, redisplay form 
     return View("Index", new LoginPageViewModel() { Login = model }); 
    } 
} 
+0

ありがとうございますが、これはテーブルaspuserclaimsのデータをデータベースに保存します。また、認証クッキーにデータを保存しますか?クッキーにのみデータを保存する方法はありますか?テーブルには保存しないでください。私は実際にデータを必要とするたびにデータベースを呼びたいとは思っていません。私はセッションなしで仕事をしたいです。 – EricImhauser

+0

はい、クッキー認証を使用する場合、認証クッキーにクレームを格納します。 –

+0

ConfigureServicesメソッド? – EricImhauser

4

を私は(私はちょうど今、この方法を学んだ)@アクアの答えを読む前に、私は次の2つのオプションがあることを言う:

1 - オーバーライドをUserClaimsPrincipalFactory以下のように:

public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> 
{ 
    public AppClaimsPrincipalFactory(
     UserManager<ApplicationUser> userManager, 
     RoleManager<IdentityRole> roleManager, 
     IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor) 
    { 
    } 

    public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user) 
    { 
     var principal = await base.CreateAsync(user); 

     ((ClaimsIdentity)principal.Identity).AddClaims(new[] { 
      new Claim("<claim name>", value) 
     }); 

     return principal; 
    } 
} 

// register it 
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>(); 

2- OnSigningInイベントを使用します。 http://www.asp.net/identity/overview/getting-started/introductionあなたは([この]のようなものを参照している - あなたは「EFでASP.NETのアイデンティティ」によって何を意味するか

 services.Configure<IdentityOptions>(opt => 
     { 
      opt.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() 
      { 
       OnSigningIn = async (context) => 
       { 
        ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity; 
        identity.AddClaim(new Claim("<claim name>", value)); 
       } 
      }; 
     }); 
+0

私はこれらのオプションを知らなかった –

関連する問題