public JwtIdentity GetRefreshToken(string refreshToken) { JwtIdentity identity = new JwtIdentity(); var tokenData = repository.GetRefreshToken(refreshToken); if (tokenData != null) { identity.RefreshToken = Mapper.Map <RefreshToken>(tokenData); identity.User = tokenData.User; } return(identity); }
public static JwtResponse Produce(JwtIdentity identity, TokenProviderOptions options) { var now = System.DateTime.UtcNow; var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Email, identity.User.Email), new Claim(JwtRegisteredClaimNames.Sub, identity.User.UserName), new Claim(JwtRegisteredClaimNames.Jti, options.NonceGenerator()), new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(now).ToUniversalTime().ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64), }; if (identity.Claims != null) { claims.AddRange(identity.Claims); claims.Add(AddIosSpecificClaims(identity.Claims)); } if (identity.Roles != null && identity.Roles.Any()) { var roleClaims = identity.Roles.Select(x => new Claim(ClaimTypes.Role, x)); claims.AddRange(roleClaims); claims.Add(AddIosSpecificRoles(identity.Roles)); } var jwt = new JwtSecurityToken( issuer: options.Issuer, audience: options.Audience, claims: claims, notBefore: now, expires: now.Add(options.Expiration), signingCredentials: options.SigningCredentials); var jwtTokenHandler = new JwtSecurityTokenHandler(); //jwtTokenHandler.InboundClaimTypeMap.Clear(); var encodedJwt = jwtTokenHandler.WriteToken(jwt); if (identity.RefreshToken == null) { return(null); } var response = new JwtResponse { AccessToken = encodedJwt, ExpiresIn = (int)options.Expiration.TotalSeconds, RefreshToken = identity.RefreshToken.Token }; return(response); }
public async Task <dynamic> Update([FromBody] ShippingTypeUpdate item) { if (item == null) { return new { JsonString = "Error" } } ; var currentUser = JwtIdentity.UserInfo(Thread.CurrentPrincipal.Identity); //item.SubmiterUserId = currentUser.Id; var result = await _sqlData.ShippingType.Update(item); return(new { Result = JsonConvert.DeserializeObject(result) }); }
public ActionResult GetToken() { string token = ""; if (Session["token"] != null) { token = Session["token"].ToString(); } var status = JwtIdentity.GetJwtDecode(token); ViewBag.status = status; return(View("Login")); }
public ActionResult SetToken() { var payload = new Dictionary <string, object> { { "username", "admin" }, { "IP", "127.0.0.1" }, { "Browser", "IE " } }; Session["token"] = JwtIdentity.SetJwtEncode(payload); ViewBag.token = Session["token"]; return(View("Login")); }
public RefreshToken GenerateRefreshToken(JwtIdentity identity) { var now = DateTime.UtcNow; var refreshToken = new RefreshToken { Token = Guid.NewGuid().ToString(), IssuedUtc = now, }; refreshToken.ExpiresUtc = now.AddDays(300); identityService.PersistRefreshToken(refreshToken, identity.User); return(refreshToken); }
public override void OnAuthorization(HttpActionContext actionContext) { AuthenticationHeaderValue authorization = actionContext.Request.Headers.Authorization; if (authorization == null || authorization.Scheme != "Basic") { this.Unauthorized(actionContext); return; } Identity identity = this.GetIdentity(authorization.Parameter, actionContext); if (identity == null || !this.ValidIdentity(identity) || identity.Client.IsBlocked) { this.Unauthorized(actionContext); return; } JwtIdentity jwtIdentity = new JwtIdentity(identity); actionContext.RequestContext.Principal = jwtIdentity.GetPrincipal(); }