示例#1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (StubUser == null)
            {
                context.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());
                return;
            }
            ;

            var claims = new[]
            {
                new Claim("Id", StubUser.Id.ToString()),
                new Claim(ClaimTypes.Name, StubUser.Name),
            };

            var identity  = new AuthenticatedIdentity(claims);
            var principal = new ClaimsPrincipal(identity);

            context.HttpContext.User = principal;
        }
        protected override async Task <string> OnCreate(String entity, CancellationToken cancellationToken = default(CancellationToken), IDictionary <string, object> parameters = null)
        {
            await Task.Delay(0);

            if (!parameters.ContainsKey("AuthenticatedIdentity"))
            {
                throw new ArgumentNullException("AuthenticatedIdentity",
                                                "AuthenticatedIdentity required in parameters");
            }
            AuthenticatedIdentity identity =
                (AuthenticatedIdentity)parameters["AuthenticatedIdentity"];

            var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));

            //TODO Implement Claims Provider
            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, identity.Id),
                new Claim(JwtRegisteredClaimNames.Jti, await _config.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat,
                          DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString(), ClaimValueTypes.Integer64)
            };

            // Create the JWT security token and encode it.
            var jwt = new JwtSecurityToken(
                issuer: _config.Issuer,
                audience: "Authenticated User",
                claims: claims,
                notBefore: _config.NotBefore,
                expires: _config.Expiration,
                signingCredentials: _config.SigningCredentials);

            string encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            //String returnValue = encodedJwt;
            return(encodedJwt);
        }