Created
August 31, 2020 15:36
-
-
Save CoskunKurtuldu/dfa7107685de439b38a457e33940c0e0 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 Microsoft.Extensions.DependencyInjection; | |
using MusicMarket.Api.Settings; | |
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.IdentityModel.Tokens; | |
using System.Text; | |
using System; | |
using Microsoft.AspNetCore.Builder; | |
namespace MusicMarket.Api.Extensions | |
{ | |
public static class AuthExtensions | |
{ | |
public static IServiceCollection AddAuth( | |
this IServiceCollection services, | |
JwtSettings jwtSettings) | |
{ | |
services | |
.AddAuthorization() | |
.AddAuthentication(options => | |
{ | |
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}) | |
.AddJwtBearer(options => | |
{ | |
options.RequireHttpsMetadata = false; | |
options.TokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidIssuer = jwtSettings.Issuer, | |
ValidAudience = jwtSettings.Issuer, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Secret)), | |
ClockSkew = TimeSpan.Zero | |
}; | |
}); | |
return services; | |
} | |
public static IApplicationBuilder UseAuth(this IApplicationBuilder app) | |
{ | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment