-
Notifications
You must be signed in to change notification settings - Fork 52
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 #214 from fluentcms/213-move-addauthentication-cod…
…e-from-programcs-to-api-project #213 AddJwtAuthentication
- Loading branch information
Showing
3 changed files
with
49 additions
and
32 deletions.
There are no files selected for viewing
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,48 @@ | ||
using FluentCMS.Providers.Identity; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace FluentCMS.Api; | ||
|
||
public static class JwtServiceExtensions | ||
{ | ||
public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IConfigurationManager configurationManager) | ||
{ | ||
var options = configurationManager.GetInstance<JwtOptions>("Jwt") ?? | ||
throw new ArgumentNullException("Jwt options in config file is not defined!"); | ||
|
||
services.AddAuthentication(configureOptions => | ||
{ | ||
var defaultScheme = "Bearer"; | ||
configureOptions.DefaultAuthenticateScheme = defaultScheme; | ||
configureOptions.DefaultScheme = defaultScheme; | ||
configureOptions.DefaultChallengeScheme = defaultScheme; | ||
}) | ||
.AddJwtBearer(jwt => | ||
{ | ||
var key = Encoding.UTF8.GetBytes(options.Secret); | ||
jwt.SaveToken = true; | ||
jwt.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(key), | ||
ValidateIssuer = true, | ||
ValidateAudience = true, | ||
RequireExpirationTime = true, | ||
ValidateLifetime = true, | ||
SaveSigninToken = true, | ||
ValidAudience = options.Audience, | ||
ValidIssuer = options.Issuer | ||
}; | ||
}); | ||
|
||
return services; | ||
} | ||
|
||
} |
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
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