Ejemplo n.º 1
0
        public static string CreateJwtToken(this ILoginInfo info)
        {
            var configKey = Config.Get("Authentication:JWT:Secret");

            if (configKey.IsEmpty() || configKey.Count() != 21)
            {
                throw new ArgumentException("Your Authentication:JWT:Secret key needs to be 21 characters.");
            }

            var securityKey = Config.Get("Authentication:JWT:Secret").ToBytes(encoding: Encoding.UTF8);

            var descriptor = new SecurityTokenDescriptor
            {
                Subject            = info.ToClaimsIdentity(),
                Issuer             = Context.Request.GetWebsiteRoot(),
                Audience           = Context.Request.GetWebsiteRoot(),
                Expires            = DateTime.UtcNow.Add(info.Timeout),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(securityKey), SecurityAlgorithms.HmacSha256),
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(descriptor);

            return(tokenHandler.WriteToken(token));
        }
Ejemplo n.º 2
0
        public static async Task LogOn(this ILoginInfo loginInfo, bool remember = false)
        {
            await Context.Http.SignOutAsync();

            var prop = new AuthenticationProperties
            {
                IsPersistent = remember,
                ExpiresUtc   = DateTimeOffset.UtcNow.Add(loginInfo.Timeout)
            };

            await Context.Http.SignInAsync(new ClaimsPrincipal(loginInfo.ToClaimsIdentity()), prop);
        }
        public static async Task LogOn(this ILoginInfo @this, IEnumerable <Claim> additionalClaims = null, bool remember = false)
        {
            var prop = new AuthenticationProperties
            {
                IsPersistent = remember,
                ExpiresUtc   = DateTimeOffset.UtcNow.Add(@this.Timeout ?? DistantFuture)
            };

            var identity = @this.ToClaimsIdentity();

            identity.AddClaims(additionalClaims.OrEmpty());
            identity.AddClaim(new Claim(ClaimTypes.IsPersistent, remember.ToString()));

            await Context.Current.Http().SignInAsync(new ClaimsPrincipal(identity), prop);
        }
Ejemplo n.º 4
0
        public static string CreateJwtToken(this ILoginInfo @this)
        {
            var securityKey = OAuth.GetJwtSecurityKey();

            var descriptor = new SecurityTokenDescriptor
            {
                Subject            = @this.ToClaimsIdentity(),
                Issuer             = Context.Current.Request().RootUrl(),
                Audience           = Context.Current.Request().RootUrl(),
                Expires            = DateTime.UtcNow.Add(@this.Timeout ?? DistantFuture),
                SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256),
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(descriptor);

            return(tokenHandler.WriteToken(token));
        }