Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created January 25, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/08bb53ba102d53ab7dcbdb1685299cf1 to your computer and use it in GitHub Desktop.
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
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