-
-
Save manoj-choudhari-git/08bb53ba102d53ab7dcbdb1685299cf1 to your computer and use it in GitHub Desktop.
AuthController For AspNetCoreIdentity Has Basic Implementation For User Registration, Login and Logout
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using CookieAuthSampleAPI.Models; | |
using Microsoft.AspNetCore.Authentication; | |
using Microsoft.AspNetCore.Authentication.Cookies; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Mvc.ModelBinding; | |
namespace CookieAuthSampleAPI.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class AuthController : ControllerBase | |
{ | |
private readonly SignInManager<IdentityUser> signInManager; | |
private readonly UserManager<IdentityUser> userManager; | |
public AuthController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager) | |
{ | |
this.signInManager = signInManager; | |
this.userManager = userManager; | |
} | |
[HttpPost] | |
[Route("Register")] | |
public async Task<IActionResult> Register([FromBody]UserDetails userDetails) | |
{ | |
if (!ModelState.IsValid || userDetails == null) | |
{ | |
return new BadRequestObjectResult(new { Message = "User Registration Failed" }); | |
} | |
var identityUser = new IdentityUser() { UserName = userDetails.UserName, Email = userDetails.Email }; | |
var result = await userManager.CreateAsync(identityUser, userDetails.Password); | |
if (!result.Succeeded) | |
{ | |
var dictionary = new ModelStateDictionary(); | |
foreach (IdentityError error in result.Errors) | |
{ | |
dictionary.AddModelError(error.Code, error.Description); | |
} | |
return new BadRequestObjectResult(new { Message = "User Registration Failed", Errors = dictionary }); | |
} | |
return Ok(new { Message = "User Reigstration Successful" }); | |
} | |
[HttpPost] | |
[Route("Login")] | |
public async Task<IActionResult> Login([FromBody]LoginCredentials credentials) | |
{ | |
if (!ModelState.IsValid || credentials == null) | |
{ | |
return new BadRequestObjectResult(new { Message = "Login failed" }); | |
} | |
var identityUser = await userManager.FindByNameAsync(credentials.Username); | |
if (identityUser == null) | |
{ | |
return new BadRequestObjectResult(new { Message = "Login failed" }); | |
} | |
var result = userManager.PasswordHasher.VerifyHashedPassword(identityUser, identityUser.PasswordHash, credentials.Password); | |
if (result == PasswordVerificationResult.Failed) | |
{ | |
return new BadRequestObjectResult(new { Message = "Login failed" }); | |
} | |
var claims = new List<Claim> | |
{ | |
new Claim(ClaimTypes.Email, identityUser.Email), | |
new Claim(ClaimTypes.Name, identityUser.UserName) | |
}; | |
var claimsIdentity = new ClaimsIdentity( | |
claims, CookieAuthenticationDefaults.AuthenticationScheme); | |
await HttpContext.SignInAsync( | |
CookieAuthenticationDefaults.AuthenticationScheme, | |
new ClaimsPrincipal(claimsIdentity)); | |
return Ok(new { Message = "You are logged in" }); | |
} | |
[HttpPost] | |
[Route("Logout")] | |
public async Task<IActionResult> Logout() | |
{ | |
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); | |
return Ok(new { Message = "You are logged out" }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment