Ejemplo n.º 1
0
        public IActionResult Sign([FromQuery] string token)
        {
            var raw      = string.Empty;
            var unixtime = MethodExtension.GetTimestamp();

            using (var stream = new StreamReader(HttpContext.Request.Body))
            {
                raw = stream.ReadToEnd();
            }

            var sign = ASEService.ToMD5($"{raw}{unixtime}{token}").ToLower();

            var result = new Result
            {
                Token    = token,
                UnixTime = unixtime.ToString(),
                Sign     = sign
            };

            return(new JsonResult(result));
        }
Ejemplo n.º 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            AuthenticationHeaderValue authHeader;

            if (!AuthenticationHeaderValue.TryParse(Request.Headers["Authorization"], out authHeader))
            {
                return(AuthenticateResult.Fail("Get Out"));
            }

            var schema = authHeader.Scheme;
            var token  = authHeader.Parameter;

            string[] credentials;

            if (schema.Equals("Basic", StringComparison.OrdinalIgnoreCase))
            {
                byte[] bytes = Convert.FromBase64String(token);
                credentials = Encoding.UTF8.GetString(bytes).Split(":");
                if (credentials.Length != 2)
                {
                    return(AuthenticateResult.Fail("Get Out"));
                }

                var clientId       = credentials[0];
                var clientPassword = credentials[1];

                if (!await _auth.CheckClientCredential(clientId, clientPassword))
                {
                    return(AuthenticateResult.Fail("Get Out"));
                }

                string[] userArray = new string[] { clientId, clientPassword, MethodExtension.GetTimestamp().ToString() };
                var      claims    = new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, clientId),
                    new Claim(ClaimTypes.NameIdentifier, clientPassword),
                    new Claim("Token", MethodExtension.EncryptToken(userArray))
                };
                var identify = new ClaimsIdentity(claims, nameof(OAuthHandler));
                var ticket   = new AuthenticationTicket(new ClaimsPrincipal(identify), Scheme.Name);

                return(AuthenticateResult.Success(ticket));
            }

            if (schema.Equals("Bearer", StringComparison.OrdinalIgnoreCase))
            {
                credentials = MethodExtension.DecryptToken(token).Split(':');
                if (credentials.Count() < 3)
                {
                    return(AuthenticateResult.Fail("Get Out"));
                }
                var clientId       = credentials[0];
                var clientPassword = credentials[1];
                var timestamp      = credentials[2];

                if (!await _auth.CheckClientCredential(clientId, clientPassword))
                {
                    return(AuthenticateResult.Fail("Get Out"));
                }

                if (!MethodExtension.CheckTimestamp(timestamp, 60))
                {
                    return(AuthenticateResult.Fail("Get Out"));
                }

                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, clientId),
                    new Claim(ClaimTypes.NameIdentifier, clientPassword)
                };
                var identify = new ClaimsIdentity(claims, nameof(OAuthHandler));
                var ticket   = new AuthenticationTicket(new ClaimsPrincipal(identify), Scheme.Name);

                return(AuthenticateResult.Success(ticket));
            }
            return(AuthenticateResult.NoResult());
        }