public JwtBearerAuthenticationFixture()
        {
            var keyByteArray = Encoding.ASCII.GetBytes("Y2F0Y2hlciUyMHdvbmclMjBsb3ZlJTIwLm5ldA==");
            var signingKey   = new SymmetricSecurityKey(keyByteArray);

            var tokenValidationParameters = new TokenValidationParameters
            {
                // The signing key must match!
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                // Validate the JWT Issuer (iss) claim
                ValidateIssuer = true,
                ValidIssuer    = "http://www.c-sharpcorner.com/members/catcher-wong",

                // Validate the JWT Audience (aud) claim
                ValidateAudience = true,
                ValidAudience    = "Catcher Wong",

                // Validate the token expiry
                ValidateLifetime = true,

                ClockSkew = TimeSpan.Zero
            };

            this.config = new JwtBearerAuthenticationConfiguration()
            {
                TokenValidationParameters = tokenValidationParameters
            };
            this.hooks = new Pipelines();
            JwtBearerAuthentication.Enable(this.hooks, this.config);
        }
Exemple #2
0
        public IResult Auth(string username, string password)
        {
            var verfiyResult = _Account.Verify(new VerifyUserDto {
                UserName     = username,
                PasswordHash = password
            });

            if (!verfiyResult.IsSuccess())
            {
                if (verfiyResult.Exception is NotFoundException)
                {
                    return(Results.InvalidIdentity);
                }
                else
                {
                    return(verfiyResult);
                }
            }

            var user = verfiyResult.Data;

            if (null == user)
            {
                return(Results.InternalError);
            }

            if (user.Status == Status.Forbidden)
            {
                return(Results.ForbiddenAccount);
            }

            if (user.IsLocked ?? false)
            {
                return(Results.LockedAccount);
            }

            var claimsIdentity = new ClaimsIdentity(new Claim[] {
                new Claim(ClaimTypes.NameIdentifier, username),
                new Claim(nameof(user.UserName), user.UserName),
                new Claim(nameof(user.UserID), user.UserID.ToString()),
                new Claim(nameof(user.Uid), user.Uid),
                new Claim(nameof(user.NickName), user.NickName),
                new Claim(nameof(user.UserType), ((int)user.UserType).ToString()),
                new Claim(nameof(user.Avatar), user.Avatar),
            });

            var token = JwtBearerAuthentication.GetJwtAccessToken(claimsIdentity);

            //HttpContext.Session.SetString (Constants.KEY_ACCESS_TOKEN, token);
            HttpContext.Response.Cookies.Append(Constants.KEY_ACCESS_TOKEN, token, _CookieOptions);

            var result = ErrorCode.Success.ToResult <Result <string> > ();

            result.Data = token;
            return(result);
        }