2

I have a Login Page with three fields Username, Password and a dropdown list. The User is supposed to a select a value from dropdown list before signing.

I need to store the selected value of dropdown list in the Authentication Cookie and Able to Retrieve it on further requests.

Using AspNetCore Identity 2.0.0 for authentication.

1 Answer 1

2

You can read up on how to do this on https://learn.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);

Eh voila. Done.

Sign up to request clarification or add additional context in comments.

1 Comment

Only that might not work as Identity expects a certain set of defined steps for authentication. I highly recommend ask Identity to generate claims and add your custom values later. Also when SecurityStampInvalidator kicks in, your custom claims will be replaced by a standard cookie -> that'll lead to obscure bugs. So you need to implement OnRefreshingPrincipal event to preserve your values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.