File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
- using Microsoft . AspNetCore . Http ;
1
+ using Microsoft . AspNetCore . Authorization ;
2
+ using Microsoft . AspNetCore . Http ;
2
3
using Microsoft . AspNetCore . Mvc ;
3
4
using MoviesApi . Services ;
4
5
5
6
namespace MoviesApi . Controllers
6
7
{
7
8
[ Route ( "api/[controller]" ) ]
8
9
[ ApiController ]
10
+ [ Authorize ]
9
11
public class GenresController : ControllerBase
10
12
{
11
13
private readonly IGenresService _genresService ;
Original file line number Diff line number Diff line change
1
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
2
+ using Microsoft . IdentityModel . Tokens ;
3
+ using System . Text ;
4
+
5
+ namespace MoviesApi . Extentions
6
+ {
7
+ public static class CustomJwtAuthExtention
8
+ {
9
+ public static void AddCustomJwtAuth ( this IServiceCollection services , ConfigurationManager configuration )
10
+ {
11
+ services . AddAuthentication ( o =>
12
+ {
13
+ o . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
14
+ o . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
15
+ o . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
16
+ } ) . AddJwtBearer ( o =>
17
+ {
18
+ o . RequireHttpsMetadata = false ;
19
+ o . SaveToken = true ;
20
+ o . TokenValidationParameters = new TokenValidationParameters ( )
21
+ {
22
+ ValidateIssuer = true ,
23
+ ValidIssuer = configuration [ "JWT:Issuer" ] ,
24
+ ValidateAudience = false ,
25
+ ValidateIssuerSigningKey = true ,
26
+ IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( configuration [ "JWT:SecretKey" ] ) )
27
+ } ;
28
+ } ) ;
29
+ }
30
+ }
31
+ }
Original file line number Diff line number Diff line change 1
1
using Microsoft . AspNetCore . Identity ;
2
2
using Microsoft . EntityFrameworkCore ;
3
3
using Microsoft . OpenApi . Models ;
4
+ using MoviesApi . Extentions ;
4
5
using MoviesApi . Models ;
5
6
using MoviesApi . Services ;
6
7
using System ;
16
17
17
18
// for jwt
18
19
builder . Services . AddIdentity < AppUser , IdentityRole > ( ) . AddEntityFrameworkStores < ApplicationDbContext > ( ) ;
20
+
21
+ builder . Services . AddCustomJwtAuth ( builder . Configuration ) ;
19
22
// Add services to the container.
20
23
21
24
builder . Services . AddTransient < IGenresService , GenresService > ( ) ;
You can’t perform that action at this time.
0 commit comments