{"id":19312056,"url":"https://github.com/weblineindia/dn-jwt-authentication","last_synced_at":"2026-05-11T16:02:03.071Z","repository":{"id":208760117,"uuid":"715601506","full_name":"weblineindia/DN-JWT-Authentication","owner":"weblineindia","description":"This is a lightweight and easy-to-use library for JSON Web Token (JWT) authentication in .NET Core applications. It provides a seamless integration of JWT-based authentication, enabling secure and efficient user authentication processes in your .NET Core projects.","archived":false,"fork":false,"pushed_at":"2024-01-09T19:15:33.000Z","size":13,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-06T02:26:54.535Z","etag":null,"topics":["dotnet","dotnet-core","jwt","jwt-authentication","jwt-token","library","security"],"latest_commit_sha":null,"homepage":"https://www.weblineindia.com/software-development-resources.html","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weblineindia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-11-07T13:26:14.000Z","updated_at":"2024-01-11T11:17:01.000Z","dependencies_parsed_at":"2023-11-23T06:35:14.397Z","dependency_job_id":null,"html_url":"https://github.com/weblineindia/DN-JWT-Authentication","commit_stats":null,"previous_names":["weblineindia/dn-jwt-authentication"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weblineindia%2FDN-JWT-Authentication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weblineindia%2FDN-JWT-Authentication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weblineindia%2FDN-JWT-Authentication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weblineindia%2FDN-JWT-Authentication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weblineindia","download_url":"https://codeload.github.com/weblineindia/DN-JWT-Authentication/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240415215,"owners_count":19797599,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","dotnet-core","jwt","jwt-authentication","jwt-token","library","security"],"created_at":"2024-11-10T00:32:33.540Z","updated_at":"2026-05-11T16:02:03.007Z","avatar_url":"https://github.com/weblineindia.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"WLI.JwtAuthentication :\n======================\nThe **WLI.JwtAuthentication** Library for .NET Core provides easy-to-use methods for generating JSON Web Tokens (JWT) and adding JWT-based authentication to your applications. This library simplifies the implementation of secure authentication and authorization mechanisms.\n\nFeatures :\n-----------\n- Generate JWT tokens with customizable parameters.\n- Refresh Jwt tokens with customizable parameters.\n- Integrate JWT-based authentication into your application's services.\n\n`JwtTokenOptions` Class :\n=====================\nThe `JwtTokenOptions` class, located in the **WLI.JwtAuthentication** namespace, provides a streamlined way to configure JWT authentication settings for use with the Token Generation Library. This class encapsulates various options that can be customized according to your application's needs.\n\n### Properties :\n\n- `Key`: The secret key used for token signing. This key ensures the authenticity and integrity of your tokens. It's recommended to use a strong and securely managed key.\n- `Issuer`: The issuer of the token, indicating the entity responsible for generating and signing the token. This value helps verify the token's source.\n- `Audience`: The intended audience for the token, specifying who the token is intended for. This value can help prevent tokens from being misused by unintended recipients.\n- `ValidateActor`: A boolean indicating whether to validate the token actor. The token actor represents the user on whose behalf the token is issued. Enabling this option adds an extra layer of validation. By default its value is true if you want then you can make it false.\n- `ValidateIssuer`: A boolean indicating whether to validate the token issuer. Validation ensures that the token's issuer matches the expected value. By default its value is true if you want then you can make it false.\n- `ValidateAudience`: A boolean indicating whether to validate the token audience. Validation ensures that the token's audience matches the intended value. By default its value is true if you want then you can make it false.\n- `ValidateLifetime`: A boolean indicating whether to validate the token's expiration time. Enabling this option helps ensure that expired tokens are rejected. By default its value is true if you want then you can make it false.\n- `ValidateIssuerSigningKey`: A boolean indicating whether to validate the issuer signing key. This validation confirms that the key used for token signing is valid and authorized. By default its value is true if you want then you can make it false.\n- `RequireExpirationTime`: A boolean indicating whether to require an expiration time in the token. Enabling this option helps prevent tokens from being used indefinitely. By default its value is true if you want then you can make it false.\n- `Events`: Allows you to specify custom event handlers for JWT authentication events.\n\n`GenerateJwtTokenOptions` Class :\n==================================\nThe `GenerateJwtTokenOptions` class, extending the `CommonOptions` class from the `WLI.JwtAuthentication.JwtTokenOptions` namespace, provides a comprehensive set of options for generating JWT tokens using the Token Generation Library. These options allow you to customize various aspects of token generation, such as token expiration times, claims, and more.\n\n### Properties :\n\n- `Key`, `Issuer`, `Audience`, `ValidateActor`, `ValidateIssuer`, `ValidateAudience`, `ValidateLifetime`, `ValidateIssuerSigningKey`, and `RequireExpirationTime`: Inherited from the `CommonOptions` class, these properties allow you to configure the common JWT authentication settings for token generation.\n\n- `Email`: The email associated with the user for whom the token is being generated. This can be used to include user-specific information in the token payload.\n\n- `RefreshToken`: The refresh token used for generating a new access token. This allows you to refresh an access token without requiring the user to log in again.\n\n- `Role`: The role or roles assigned to the user. Roles can be used to implement fine-grained authorization within your application.\n\n- `AccessTokenExpirationTime`: The expiration time (in seconds) for the access token. This defines the period of validity for the access token.\n\n- `RefreshTokenExpirationTime`: The expiration time (in seconds) for the refresh token. This defines the period of validity for the refresh token.\n\n- `Claims`: A list of `Claim` objects that you can use to include additional information in the token payload. Claims represent statements about the user and their roles, rights, or other attributes.\n\n`IJwtTokenService` Interface :\n=================================\nThe IJwtTokenService contains two non static method which is -\n- public Tokens JwtToken(GenerateJwtTokenOptions options);\n- public Tokens RefreshAccessToken(GenerateJwtTokenOptions options);\n\n`JwtTokenService` Class :\n============================\nThe `JwtTokenService` class in the **WLI.JwtAuthentication** library serves as a core component for generating and refreshing JSON Web Tokens (JWT). This class provides methods to create both access tokens and refresh tokens with customizable parameters. This class is Inherited from IJwtTokenGeneration interface.\n\n## Registering the IJwtTokenService Service :\nTo utilize the `IJwtTokenService` service in your ASP.NET Core application, you need to register it in the `Program.cs` file. This service is responsible for generating and refreshing JWT tokens.\n\nHere's how you can register the `IJwtTokenService` service in your `Program.cs` file:\n```bash\n    builder.Services.AddScoped\u003cIJwtTokenService, JwtTokenService\u003e();\n```\n\n### The Methods which are present inside the class are :\n- JwtToken.\n- RefreshAccessToken.\n\nJwtToken Method :\n==================\nIn `JwtTokenService` class there is an non static method which name is **JwtToken**. It will returns an Token object which contains access Jwt Token and refresh JWT Token. We have to set the parameter for it.\n\n### Parameters\n- **options:** An instance of the `GenerateJwtTokenOptions` class containing the necessary parameters for token generation:\n   - `Claims:` The claims to include in the token payload.\n   - `AccessTokenExpirationTime:` The expiration time for the access token(in minutes).\n   - `RefreshTokenExpirationTime:` The expiration time for the refresh token(in minutes).\n   -  `Key:` The secret key used for token signing.\n   -  `Audience:` The intended audience for the tokens.\n   -  `Issuer:` The issuer of the tokens.\n\n### Exmaple to call this method :\n- Create instance of `IJwtTokenService` like _jwtTokenService.\n- For getting value of `Key`, `Issuer` and `Audeince` create an instance of Configurable class like _config.\n\n- You can get you `Key`, `Issuer`, `Audience` from your appsettings.json file.\n```bash\n var options = new GenerateJwtTokenOptions\n {\n     Email = \"test@gmail.com\",\n     Issuer = _config.GetSection(\"Jwt:Issuer\").Value, // Get your Issuer from your appsettings.json file e.g. \"https://localhost:7129\"\n     Audience = _config.GetSection(\"Jwt:Audience\").Value, // Get your Audience from your appsettings.json file e.g. \"https://localhost:7129\"\n     Key = _config.GetSection(\"Jwt:Key\").Value // Get your Key from your appsettings.json file e.g. key:4F9A5D6E8B1C2F7D9A5D6E8B1C2F\n     Role = \"TestRole\",\n     AccessTokenExpirationTime = Convert.ToInt32(2),//In Minutes\n     RefreshTokenExpirationTime = Convert.ToInt32(5),//In Minutes\n     Claims = new List\u003cClaim\u003e\n     {\n         new Claim(ClaimTypes.Email, \"test@gmail.com\"),\n         new Claim(ClaimTypes.Role, \"TestRole\")\n     }\n     // You can specific validations for it from CommonOptions class\n     ValidateIssuer = true;\n     ValidateAudience = ture;\n     Event = new JwtBearerEvents(){\n          OnChallenge = async context =\u003e\n          {\n            context.HandleResponse();\n            context.Response.StatusCode = 400;\n            context.Response.ContentType = \"application/json\";\n            \n            await context.Response.WriteAsync(JsonConvert.SerializeObject(\"Custom object\", jsonSerialize));\n         }\n     }\n };\n```\n\n### Method Calling :\n    _jwtTokenService.JwtToken(options);\n### Method Signature :\n        public Tokens JwtToken(GenerateJwtTokenOptions options);\n\n### Installation:\n- Open your project in Visual Studio or your preferred code editor.\n\n- Add a reference to the JWT Token Authentication Library in your project.\n- Import the library namespace in the classes where you want to use it.\n   - using WLI.JwtAuthentication;\n- Create instance of IJwtTokenService like _jwtTokenService\n- Call the Method using Class Name.\n   - _jwtTokenService.JwtToken(options);\n\n### Return Value :\nThe method returns a Tokens object containing the newly generated access token and refresh token.\n\nRefreshAccessToken  Method :\n============================\nThe `RefreshAccessToken` method in the **WLI.JwtAuthentication** library is responsible for refreshing an access token using a provided refresh token. This method simplifies the process of renewing tokens without the need for users to re-authenticate.\n\n### Parameters :\n- **options:** An object of type GenerateJwtTokenOptions that includes the required parameters for token generation:\n   - `Claims:` The claims to be included in the token payload.\n   - `AccessTokenExpirationTime:` The access token's expiration time.\n   - `RefreshTokenExpirationTime:` The refresh token's expiration time.\n   -  `Key:` The secret key for token signing.\n   -  `Audience:` The intended audience for the tokens.\n   -  `Issuer:` The issuer of the tokens.\n\n### Exmaple to call this method : \n- Create instance of `IJwtTokenService` like _jwtTokenService.\n- For getting value of `Key`, `Issuer` and `Audeince` create an instance of Configurable class like _config.\n```bash\nvar options = new GenerateJwtTokenOptions\n{\n    RefreshToken = refreshToken,\n    Issuer = _config.GetSection(\"Jwt:Issuer\").Value, // Get your Issuer from your appsettings.json file e.g. \"https://localhost:7129\"\n    Audience = _config.GetSection(\"Jwt:Audience\").Value, // Get your Audience from your appsettings.json file e.g. \"https://localhost:7129\"\n    Key = _config.GetSection(\"Jwt:Key\").Value // Get your Key from your appsettings.json file e.g. key:4F9A5D6E8B1C2F7D9A5D6E8B1C2F\n    AccessTokenExpirationTime = Convert.ToInt32(2),//In Minutes\n    RefreshTokenExpirationTime = Convert.ToInt32(5),//In Minutes\n};\n```\n### Method Calling :\n    _jwtTokenService.RefreshAccessToken(options);\n\n### Method Signature :\n        public Tokens RefreshAccessToken(GenerateJwtTokenOptions options);\n\n## Return Value :\nThe method returns a Tokens object containing the newly generated access token and refresh token.\n\n`JwtAuthentication` Class :\n===========================\nIn JwtAuthentication Class there is an static method which name is AddJwtAuthentication().The AddJwtAuthentication method simplifies the process of adding JWT-based authentication to your application's services using the ASP.NET Core IServiceCollection.\n\n### Adding JWT Authentication in Program.cs :\n- Import the library namespace in the Program.cs class.\n    - using WLI.JwtAuthentication;\n```bash\nvar jwtOptions = new JwtTokenOptions\n{\n    // Set your JWT configuration options here\n    ValidateActor = true,\n    ValidateAudience = true,\n    RequireExpirationTime = true,\n    ValidateIssuerSigningKey = true,\n    Issuer = builder.Configuration.GetSection(\"Jwt:Issuer\").Value, // Get your Issuer from your appsettings.json file\n    Audience = builder.Configuration.GetSection(\"Jwt:Audience\").Value, // Get your Audience from your appsettings.json file\n    Key = builder.Configuration.GetSection(\"Jwt:Key\").Value // Get your Key from your appsettings.json file\n};\n```\n### Calling Method :\nservices.AddJwtAuthentication(jwtOptions);\n\n### Method Signature :\n      public static void AddJwtAuthentication(this IServiceCollection services, JwtTokenOptions jwtOptions);\n\n### Security Considerations :\n- **Secret Key:** Keep your secret key confidential and avoid hardcoding it in your codebase.\n- **Audience and Issuer:** Ensure that the values for audience and issuer match the ones used during token generation.\n\n  ## Want to Contribute?\n\n- Created something awesome, made this code better, added some functionality, or whatever (this is the hardest part).\n- [Fork it](http://help.github.com/forking/).\n- Create new branch to contribute your changes.\n- Commit all your changes to your branch.\n- Submit a [pull request](http://help.github.com/pull-requests/).\n\n---\n\n## Collection of Components\n\nWe have built many other components and free resources for software development in various programming languages. Kindly click here to view our [Free Resources for Software Development](https://www.weblineindia.com/software-development-resources.html)\n\n---\n\n## License\n\n[MIT](LICENSE)\n\n[mit]: https://github.com/weblineindia/ReactJS-Email/blob/master/LICENSE\n\n## Keywords\n \ndotnet, jwt, authentication, login\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweblineindia%2Fdn-jwt-authentication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweblineindia%2Fdn-jwt-authentication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweblineindia%2Fdn-jwt-authentication/lists"}