Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Commit 7d761b4

Browse files
author
kahmingt
committed
Enabled JWT Authentication
1 parent e21a9b4 commit 7d761b4

File tree

4 files changed

+160
-14
lines changed

4 files changed

+160
-14
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.AspNetCore.Mvc;
3+
using API.Models;
4+
//using Microsoft.AspNet.Identity.Owin;
5+
//using Microsoft.IdentityModel.Tokens;
6+
//using Microsoft.AspNetCore.Mvc;
7+
//using Microsoft.Owin.Security;
8+
//using Microsoft.Owin.Security.Cookies;
9+
//using Microsoft.Owin.Security.OAuth;
10+
using System.IdentityModel.Tokens.Jwt;
11+
using Microsoft.IdentityModel.Tokens;
12+
using System.Text;
13+
using System.Security.Claims;
14+
15+
16+
namespace WebApi.Controllers
17+
{
18+
[Route("api/[Controller]")]
19+
public class AccountController : ControllerBase
20+
{
21+
private readonly SignInManager<IdentityUser> _signInManager;
22+
private readonly UserManager<IdentityUser> _userManager;
23+
private readonly IConfiguration _configuration;
24+
25+
public AccountController(
26+
IConfiguration configuration,
27+
SignInManager<IdentityUser> signInManager,
28+
UserManager<IdentityUser> userManager)
29+
{
30+
_configuration = configuration;
31+
_signInManager = signInManager;
32+
_userManager = userManager;
33+
}
34+
35+
private async Task<JwtSecurityToken> GenerateJsonWebToken(IdentityUser user, int expiresMinutes = 10)
36+
{
37+
// JWT Claims
38+
// - GUID
39+
// - UserName
40+
// - Roles
41+
var authClaims = new List<Claim>
42+
{
43+
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
44+
new Claim(ClaimTypes.Name, user.UserName),
45+
};
46+
47+
var userRoles = await _userManager.GetRolesAsync(user);
48+
foreach (var userRole in userRoles)
49+
{
50+
authClaims.Add(new Claim(ClaimTypes.Role, userRole));
51+
}
52+
53+
// JWT secret
54+
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));
55+
56+
// JWT expiration
57+
expiresMinutes = (expiresMinutes < 1) ? 10 : expiresMinutes;
58+
59+
return new JwtSecurityToken(
60+
issuer: _configuration["JWT:ValidIssuer"],
61+
audience: _configuration["JWT:ValidAudience"],
62+
expires: DateTime.Now.AddMinutes(expiresMinutes),
63+
claims: authClaims,
64+
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
65+
);
66+
}
67+
68+
69+
[HttpPost]
70+
[Route("login")]
71+
public async Task<IActionResult> Login([FromBody] LoginRequest LoginRequest)
72+
{
73+
if (ModelState.IsValid)
74+
{
75+
// Search by username or email
76+
var user = await _userManager.FindByNameAsync(LoginRequest.Login);
77+
user ??= await _userManager.FindByEmailAsync(LoginRequest.Login);
78+
79+
if (user == null)
80+
{
81+
return Unauthorized();
82+
}
83+
84+
var result = await _signInManager.PasswordSignInAsync(user.UserName, LoginRequest.Password, true, lockoutOnFailure: true);
85+
if (result.Succeeded)
86+
{
87+
var token = await GenerateJsonWebToken(user);
88+
89+
return Ok(new
90+
{
91+
token = new JwtSecurityTokenHandler().WriteToken(token),
92+
expiration = token.ValidTo
93+
});
94+
}
95+
return Unauthorized("Unauthorized");
96+
}
97+
return BadRequest("Invalid");
98+
}
99+
}
100+
101+
public class Response
102+
{
103+
public string Status { get; set; }
104+
public string Message { get; set; }
105+
}
106+
}

‎WebApi/Controllers/ProductController.cs‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace WebApi.Controllers
66
{
7+
[Authorize(Roles = "Admin")]
78
[ApiController]
8-
[Route("api/[Controller]")]
9+
[Route("api/[controller]")]
910
public class ProductController : ControllerBase
1011
{
1112
private readonly IRepositoryWrapper _db;
@@ -16,7 +17,6 @@ public ProductController(IRepositoryWrapper db)
1617
}
1718

1819
[HttpGet]
19-
[Route("/ProductList")]
2020
[ProducesResponseType(StatusCodes.Status200OK)]
2121
[ProducesResponseType(StatusCodes.Status400BadRequest)]
2222
public async Task<ActionResult<IEnumerable<ProductListing>>> GetProductList()
@@ -26,40 +26,42 @@ public async Task<ActionResult<IEnumerable<ProductListing>>> GetProductList()
2626
}
2727

2828
[HttpGet]
29-
[Route("{ProductId}")]
29+
[Route("{id}")]
3030
[ProducesResponseType(StatusCodes.Status200OK)]
3131
[ProducesResponseType(StatusCodes.Status404NotFound)]
32-
public async Task<ActionResult> GetProductDetailsById(int ProductId)
32+
public async Task<ActionResult> GetProductDetailsById(int id)
3333
{
34-
var model = await _db.ProductService.GetProductDetailsById(ProductId);
34+
var model = await _db.ProductService.GetProductDetailsById(id);
3535
return model == null ? NotFound("Invalid") : Ok(model);
3636
}
3737

3838
[HttpDelete]
39-
[Route("{ProductId}")]
39+
[Route("{id}")]
4040
[ProducesResponseType(StatusCodes.Status200OK)]
4141
[ProducesResponseType(StatusCodes.Status400BadRequest)]
42-
public async Task<ActionResult> DeleteProductDetailsById(int ProductId)
42+
public async Task<ActionResult> DeleteProductDetailsById(int id)
4343
{
44-
var model = await _db.ProductService.DeleteProductById(ProductId);
44+
var model = await _db.ProductService.DeleteProductById(id);
4545
return model == false ? BadRequest("Invalid") : Ok();
4646
}
4747

4848
[HttpPut]
49-
[Route("{ProductId}")]
49+
[Route("{id}")]
5050
[ProducesResponseType(StatusCodes.Status200OK)]
5151
[ProducesResponseType(StatusCodes.Status400BadRequest)]
52-
public async Task<ActionResult> UpdateProductDetailsById(int ProductId, ProductDetails productDetails)
52+
public async Task<ActionResult> UpdateProductDetailsById(int id, ProductDetails productDetails)
5353
{
54-
if (!ModelState.IsValid || ProductId != productDetails.ProductID)
54+
if (!ModelState.IsValid || id != productDetails.ProductID)
5555
{
5656
return BadRequest("Invalid");
5757
}
58-
var model = await _db.ProductService.UpdateProductDetailsById(ProductId, productDetails);
58+
var model = await _db.ProductService.UpdateProductDetailsById(id, productDetails);
5959
return model == null ? BadRequest("Invalid") : Ok(model);
6060
}
6161

6262
[HttpPost]
63+
[ProducesResponseType(StatusCodes.Status200OK)]
64+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
6365
public async Task<IActionResult> CreateProduct([FromBody] ProductDetails productDetails)
6466
{
6567
if (!(ModelState.IsValid))

‎WebApi/Models/Account.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Xml.Linq;
3+
4+
namespace API.Models
5+
{
6+
public class RegisterModel
7+
{
8+
[Required]
9+
[Display(Name = "Username")]
10+
public string Username { get; set; }
11+
12+
[Required]
13+
[EmailAddress]
14+
[Display(Name = "Email")]
15+
public string Email { get; set; }
16+
17+
[Required]
18+
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
19+
[DataType(DataType.Password)]
20+
[Display(Name = "Password")]
21+
public string Password { get; set; }
22+
23+
[DataType(DataType.Password)]
24+
[Display(Name = "Confirm password")]
25+
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
26+
public string ConfirmPassword { get; set; }
27+
}
28+
29+
public class LoginRequest
30+
{
31+
[Required]
32+
public string Login { get; set; }
33+
34+
[Required]
35+
public string Password { get; set; }
36+
}
37+
38+
}

‎WebApi/WebApi.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<PackageReference Include="Microsoft.AspNet.Identity.Owin" Version="2.2.3" />
1212
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
1313
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
14-
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
1514
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.15" />
15+
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.15" />
1616
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.15" />
1717
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.15">
1818
<PrivateAssets>all</PrivateAssets>
@@ -21,5 +21,5 @@
2121
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.13" />
2222
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
2323
</ItemGroup>
24-
24+
2525
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /