Created
April 18, 2018 00:42
-
-
Save gmartinezsan/d5c794f145928e96b9b02efbd7af853f to your computer and use it in GitHub Desktop.
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.Threading.Tasks; | |
using BooksWebApi.Data; | |
using BooksWebApi.Entities; | |
using BooksWebApi.Models; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
namespace BooksWebApi.Controllers | |
{ | |
public class AuthController : Controller | |
{ | |
private BooksCatalogDbContext _context; | |
private SignInManager<User> _signInMgr; | |
private ILogger<AuthController> _logger; | |
public AuthController(BooksCatalogDbContext context, SignInManager<User> signInMgr, ILogger<AuthController> logger) | |
{ | |
_context = context; | |
_signInMgr = signInMgr; | |
_logger = logger; | |
} | |
[HttpPost("api/Auth/login")] | |
public async Task<IActionResult> Login([FromBody] LoginModel model) | |
{ | |
try | |
{ | |
var result = await _signInMgr.PasswordSignInAsync(model.UserName, model.Password, false, false); | |
if (result.Succeeded) | |
{ | |
return Ok(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
_logger.LogError($"an Exception is thrown while logging in {ex}"); | |
} | |
return BadRequest("Failed to login"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment