-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from babisque/authorization/feature/sso-implem…
…entation Endpoint to generate Bearer Token is done.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Authorization/Authorization.API/Controllers/TokenController.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Authorization.DTO.Token; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Authorization.API.Controllers; | ||
|
||
[ApiController] | ||
[Route("/[controller]")] | ||
public class TokenController : ControllerBase | ||
{ | ||
private UserManager<IdentityUser> _userManager; | ||
|
||
public TokenController(UserManager<IdentityUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult> Post(TokenPostReq req) | ||
{ | ||
var user = await _userManager.FindByNameAsync(req.Username); | ||
if (user == null || !(await _userManager.CheckPasswordAsync(user, req.Password))) | ||
return BadRequest("User or password is incorrect."); | ||
|
||
var claims = await _userManager.GetClaimsAsync(user); | ||
var roles = await _userManager.GetRolesAsync(user); | ||
|
||
var key = Encoding.ASCII.GetBytes( | ||
"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm,1234567890AOkopvdnsioHGYUASGVBI"); | ||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(new Claim[] | ||
{ | ||
new Claim(ClaimTypes.Email, user.Email), | ||
}), | ||
SigningCredentials = | ||
new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature), | ||
Audience = "eCommerce", | ||
Issuer = "Issuer" | ||
}; | ||
|
||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
|
||
return Ok(new | ||
{ | ||
token = tokenHandler.WriteToken(token) | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Authorization.DTO.Token; | ||
|
||
public class TokenPostReq | ||
{ | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
} |