调用SignInAsync时,在身份验证cookie中保存一个值

时间:2017-11-11 07:32:07

标签: asp.net-core asp.net-identity

我有一个登录页面,其中包含三个字段:用户名,密码和下拉列表。用户应该在签名之前从下拉列表中选择一个值。

我需要在“身份验证Cookie”中存储下拉列表的选定值,并且能够在进一步请求时检索它。

使用AspNetCore Identity 2.0.0进行身份验证。

1 个答案:

答案 0 :(得分:1)

您可以在https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x

上了解如何执行此操作
//somehow authenticate your user. I don't care how you do this.
var user = await AuthenticateUser(Input.Email, Input.Password);

//create a list of claims - all you need based on your user information
//this si also where you store the information based on your ddl value
var claims = new List<Claim>
{
    new Claim(ClaimTypes.Name, user.Email),
    new Claim("FullName", user.FullName),
    new Claim("DropdownValue", /*ADD DROPDOWN VALUE HERE*/)
};

var claimsIdentity = new ClaimsIdentity(
    claims, CookieAuthenticationDefaults.AuthenticationScheme);

var authProperties = new AuthenticationProperties();


await HttpContext.SignInAsync(
      CookieAuthenticationDefaults.AuthenticationScheme, 
      new ClaimsPrincipal(claimsIdentity), 
      authProperties);
瞧瞧瞧。完成。