Movatterモバイル変換


[0]ホーム

URL:


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

.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities

License

NotificationsYou must be signed in to change notification settings

NetDevPack/Security.Identity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

.NET DevPack

What is the .NET DevPack.Identity?

.NET DevPack Identity is a set of common implementations to help you implementing ASP.NET Identity, JWT, claims validation and another facilities

Codacy BadgeBuild status.NET CoreLicense

Give a Star! ⭐

If you liked the project or if NetDevPack helped you, please give a star ;)

Get Started

PackageVersionPopularity
NetDevPack.IdentityNuGetNuget

.NET DevPack.Identity can be installed in your ASP.NET Core application using the Nuget package manager or thedotnet CLI.

dotnet add package NetDevPack.Identity

If you want to use our IdentityDbContext (ASP.NET Identity standard) you will need to create the Identity tables. Set your connection string in theappsettings.json and follow the next steps:

Add the IdentityDbContext configuration in yourstartup.cs:

services.AddIdentityEntityFrameworkContextConfiguration(options=>options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"), b=>b.MigrationsAssembly("AspNetCore.Jwt.Sample")));

Note: You must inform the namespace to avoid migration errors

Note: You must install theMicrosoft.EntityFrameworkCore.SqlServer or another provider likeNpgsql.EntityFrameworkCore.PostgreSQL package to have support from your database. Find the package for your databasehere

Add the Identity configuration inConfigureServices method of yourstartup.cs:

services.AddIdentityConfiguration();

Note: This extension returns an IdentityBuilder to allow you extending the configuration

Add the Identity configuration inConfigure method of yourstartup.cs:

app.UseAuthConfiguration();

Note: This method need to be set betweenapp.UseRouting() andapp.UseEndpoints()

Run the command to generate the migration files:

dotnet ef migrations add Initial --context NetDevPackAppDbContext --project <Your patch>/<Your Project>.csproj

Run the command to generate the database:

dotnet ef database update --context NetDevPackAppDbContext --project <Your patch>/<Your Project>.csproj

Note: If are you using your ownIdentityDbContext you must change theNetDevPackAppDbContext value to your context class name in the commands above.

After execute this steps you will be all set to use the Identity in your Application.

Configuring JWT

If you want to generate JSON Web Tokens in your application you need to add the JWT configuration inConfigureServices method of yourstartup.cs

services.AddJwtConfiguration(Configuration).AddNetDevPackIdentity<IdentityUser>();

Set yourappsettings.json file with this values:

"AppJwtSettings": {"Audience":"MyApplication.Name"}

It's possible to configure some aspects of token

KeyMeaningDefault
ExpirationExpiration time (in hours)1
IssuerThe party that "created" the token and signed it with its private key. Usually the application UrlGet current root Url fromHttpContext
AudienceAPI's that should accept the token. E.g your application Main name.NetDevPack
RefreshTokenExpirationRefresh token expiration (In Days)30
RefreshTokenTypeOneTime orReUse30
SecretKeyDeprecatedIs your key to build JWT.Read notesDo not use it

Note: Now we are usingNetDevPack.Security.Jwt to generate and Store your keys. It generate a RSA 2048 by default. You can check the project for more info.

Generating JWT

You will need to set a single dependency in your Authentication Controller:

publicAuthController(IJwtBuilder jwtBuilder){_jwtBuilder=jwtBuilder;}

After user register or login process you can generate a JWT to respond the request. Use our implementation, you just need inform the user email and the dependencies injected in your controller:

return_jwtBuilder.WithEmail(email).WithRefreshToken().BuildToken();

Note: This builder can return a single string with JWT or a complex objectUserResponse if you want return more data than a single JWT string.

Adding Claims to your JWT

You can call more methods inJwtBuilder to provide more information about the user:

return_jwtBuilder.WithEmail(email).WithJwtClaims().WithUserClaims().WithUserRoles().WithRefreshToken().BuildToken();
MethodMeaning
WithJwtClaims()Claims of JWT likesub,jti,nbf and others
WithUserClaims()The user claims registered inAspNetUserClaims table
WithUserRoles()The user roles (as claims) registered inAspNetUserRoles table
BuildToken()Build and return the JWT as single string

If you want return your complex objectUserResponse you need to change the last method to:

return_jwtBuilder.WithEmail(email).WithJwtClaims().WithUserClaims().WithUserRoles().WithRefreshToken().BuildUserResponse();

Examples

Use thesample application to understand how NetDevPack.Identity can be implemented and help you to decrease the complexity of your application and development time.

Compatibility

TheNetDevPack.Identity was developed to be implemented inASP.NET Core. It support all .NET versions since 3.1.

About

.NET DevPack.Identity was developed byEduardo Pires under theMIT license.

About

.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp