Created
December 24, 2017 12:39
-
-
Save GeorgDangl/d82f33488745c37f913caff325a7f464 to your computer and use it in GitHub Desktop.
Appending custom claims when logging in via cookie with Asp.Net Core Identity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountController : Controller | |
{ | |
[HttpPost("login")] | |
[AllowAnonymous] | |
[ProducesResponseType(204)] | |
[ProducesResponseType(400)] | |
public async Task<IActionResult> Login([FromBody] LoginPost model) | |
{ | |
var user = await _userManager.FindByEmailAsync(model.Identifier) | |
?? await _userManager.FindByNameAsync(model.Identifier); | |
if (user != null) | |
{ | |
var passwordIsCorrect = await _userManager.CheckPasswordAsync(user, model.Password); | |
if (passwordIsCorrect) | |
{ | |
var customClaims = new [] | |
{ | |
new Claim("logged_in_day", DateTime.UtcNow.DayOfWeek.ToString()) | |
}; | |
await _customClaimsCookieSignInHelper.SignInUserAsync(user, model.RememberMe, customClaims); | |
_logger.LogInformation(1, "User logged in."); | |
return NoContent(); | |
} | |
} | |
return BadRequest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment