Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

DotNet web Api Token based Authentication

using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WebApplication.Models;
namespace WebApplication.JwtHelpers {
    public static class JwtHelpers {
        public static IEnumerable < Claim > GetClaims(this UserTokens userAccounts, Guid Id) {
            IEnumerable < Claim > claims = new Claim[] {
                new Claim("Id", userAccounts.Id.ToString()),
                    new Claim(ClaimTypes.Name, userAccounts.UserName),
                    new Claim(ClaimTypes.Email, userAccounts.EmailId),
                    new Claim(ClaimTypes.NameIdentifier, Id.ToString()),
                    new Claim(ClaimTypes.Expiration, DateTime.UtcNow.AddDays(1).ToString("MMM ddd dd yyyy HH:mm:ss tt"))
            };
            return claims;
        }
        public static IEnumerable < Claim > GetClaims(this UserTokens userAccounts, out Guid Id) {
            Id = Guid.NewGuid();
            return GetClaims(userAccounts, Id);
        }
        public static UserTokens GenTokenkey(UserTokens model, JwtSettings jwtSettings) {
            try {
                var UserToken = new UserTokens();
                if (model == null) throw new ArgumentException(nameof(model));
                // Get secret key
                var key = System.Text.Encoding.ASCII.GetBytes(jwtSettings.IssuerSigningKey);
                Guid Id = Guid.Empty;
                DateTime expireTime = DateTime.UtcNow.AddDays(1);
                UserToken.Validaty = expireTime.TimeOfDay;
                var JWToken = new JwtSecurityToken(issuer: jwtSettings.ValidIssuer, audience: jwtSettings.ValidAudience, claims: GetClaims(model, out Id), notBefore: new DateTimeOffset(DateTime.Now).DateTime, expires: new DateTimeOffset(expireTime).DateTime, signingCredentials: new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256));
                UserToken.Token = new JwtSecurityTokenHandler().WriteToken(JWToken);
                UserToken.UserName = model.UserName;
                UserToken.Id = model.Id;
                UserToken.GuidId = Id;
                return UserToken;
            } catch (Exception) {
                throw;
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make dobuble jump unity 2d 
Csharp :: Visual Studio - Summary Tag Comments - Optional Params 
Csharp :: c# unary operator 
Csharp :: linq dynamic order by descending 
Csharp :: c# how to group console output into columns 
Csharp :: internet connection sharing 
Csharp :: how to backup terrain in unity 
Csharp :: unity save slots 
Csharp :: convert bool to uint in solidity 
Csharp :: MVC 5 identity SignOut Everywhere for specific user 
Csharp :: object shaking unity 
Csharp :: get access to all controls with a specific tag in C# 
Csharp :: unity wheelcollider antiroll 
Csharp :: isdaylightsavingtime in c# 
Csharp :: lizzo net worth 
Csharp :: how to write an if statement with two checkboxes in c# 
Csharp :: model showing in scne view but not in game view 
Csharp :: .net entities query multiple join condition 
Csharp :: Query Parent-GrandChild single 
Csharp :: how to handle list properties in c# of string type 
Csharp :: C# .net JwtSecurityTokenHandler jwttoken claims to object 
Csharp :: c# functions 
Csharp :: generate prime numbers 
Csharp :: call ienumerator unity 
Csharp :: unity destroy 
Csharp :: how to clear a dictionary in c# 
Csharp :: c# code process to start any exe application 
Csharp :: winforms open multiple forms show one icon in taskabr 
Csharp :: git set origin url 
Html :: bootstrap text bold 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =