Skip to content

Instantly share code, notes, and snippets.

@demonguru18
Last active February 12, 2019 02:04
Show Gist options
  • Save demonguru18/2ab12d2e5a225436b8eeb7e7fb6fbc0d to your computer and use it in GitHub Desktop.
Save demonguru18/2ab12d2e5a225436b8eeb7e7fb6fbc0d to your computer and use it in GitHub Desktop.
This is the Account Controller Class for Our Application
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using NG_Core_Auth.Helpers;
using NG_Core_Auth.Models;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace NG_Core_Auth.Controllers
{
[Route("api/[controller]")]
public class AccountController : Controller
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signManager;
private readonly AppSettings _appSettings;
public AccountController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, IOptions<AppSettings> appSettings)
{
_userManager = userManager;
_signManager = signInManager;
_appSettings = appSettings.Value;
}
[HttpPost("action")]
public async Task<IActionResult> Register([FromBody] RegisterViewModel formdata)
{
// Will hold all the errors related to registration
List<string> errorList = new List<string>();
var user = new IdentityUser
{
Email = formdata.Email,
UserName = formdata.UserName,
SecurityStamp = Guid.NewGuid().ToString()
};
var result = await _userManager.CreateAsync(user, formdata.Password);
if (result.Succeeded)
{
await _userManager.AddToRoleAsync(user, "Customer");
// Sending Confirmation Email
return Ok(new {username = user.UserName, email = user.Email, status = 1, message = "Registration Successful" });
}
else
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
errorList.Add(error.Description);
}
}
return BadRequest(new JsonResult(errorList));
}
// Login Method
[HttpPost("action")]
public async Task<IActionResult> Login([FromBody] LoginViewModel formdata)
{
// Get the User from Database
var user = await _userManager.FindByNameAsync(formdata.Username);
var roles = await _userManager.GetRolesAsync(user);
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_appSettings.Secret));
double tokenExpiryTime = Convert.ToDouble(_appSettings.ExpireTime);
if (user != null && await _userManager.CheckPasswordAsync(user, formdata.Password))
{
// Confirmation of email
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, formdata.Username),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Role, roles.FirstOrDefault()),
new Claim("LoggedOn", DateTime.Now.ToString()),
}),
SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature),
Issuer = _appSettings.Site,
Audience = _appSettings.Audience,
Expires = DateTime.UtcNow.AddMinutes(tokenExpiryTime)
};
// Generate Token
var token = tokenHandler.CreateToken(tokenDescriptor);
return Ok(new {token = tokenHandler.WriteToken(token), expiration = token.ValidTo, username = user.UserName, userRole = roles.FirstOrDefault() });
}
// return error
ModelState.AddModelError("", "Username/Password was not Found");
return Unauthorized(new { LoginError = "Please Check the Login Credentials - Ivalid Username/Password was entered" });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment