예제 #1
0
        /// <summary>
        /// This method parses the incoming token and validates it.
        /// </summary>
        /// <param name="accessToken">The incoming access token.</param>
        /// <param name="error">This out paramter is set if any error occurs.</param>
        /// <returns>True on success, False on error.</returns>
        protected bool ReadAndValidateToken(string accessToken, out ResourceAccessErrorResponse error)
        {
            bool tokenValid = false;

            error = null;

            SecurityToken            token = null;
            ClaimsIdentityCollection claimsIdentityCollection = null;

            try
            {
                var handler = new SimpleWebTokenHandler(_issuer, _tokenSigningKey);

                // read the token
                token = handler.ReadToken(accessToken);

                // validate the token
                claimsIdentityCollection = handler.ValidateToken(token, _realm);

                // create a claims Principal from the token
                var claimsPrincipal = ClaimsPrincipal.CreateFromIdentities(claimsIdentityCollection);
                if (claimsPrincipal != null)
                {
                    tokenValid = true;

                    // push it through the pipeline
                    foreach (var step in authenticationPipeline)
                    {
                        claimsPrincipal = step.Authenticate(token, claimsPrincipal);
                    }

                    // assign to threads
                    if (HttpContext.Current != null)
                    {
                        HttpContext.Current.User = claimsPrincipal;
                    }
                    Thread.CurrentPrincipal = claimsPrincipal;
                }
            }
            catch (InvalidTokenReceivedException ex)
            {
                error = new ResourceAccessErrorResponse(_realm, ex.ErrorCode, ex.ErrorDescription);
            }
            catch (ExpiredTokenReceivedException ex)
            {
                error = new ResourceAccessErrorResponse(_realm, ex.ErrorCode, ex.ErrorDescription);
            }
            catch (Exception)
            {
                error = new ResourceAccessErrorResponse(_realm, "SWT401", "Token validation failed");
            }

            return(tokenValid);
        }
        /// <summary>
        /// This method parses the incoming token and validates it.
        /// </summary>
        /// <param name="accessToken">The incoming access token.</param>
        /// <param name="error">This out paramter is set if any error occurs.</param>
        /// <returns>True on success, False on error.</returns>
        protected bool ReadAndValidateToken(string accessToken, out ResourceAccessErrorResponse error)
        {
            bool tokenValid = false;
            error = null;

            SecurityToken token = null;
            ClaimsIdentityCollection claimsIdentityCollection = null;

            try
            {
                var handler = new SimpleWebTokenHandler(_issuer, _tokenSigningKey);

                // read the token
                token = handler.ReadToken(accessToken);

                // validate the token
                claimsIdentityCollection = handler.ValidateToken(token, _realm);

                // create a claims Principal from the token
                var claimsPrincipal = ClaimsPrincipal.CreateFromIdentities(claimsIdentityCollection);
                if (claimsPrincipal != null)
                {
                    tokenValid = true;

                    // push it through the pipeline
                    foreach (var step in authenticationPipeline)
                    {
                        claimsPrincipal = step.Authenticate(token, claimsPrincipal);
                    }

                    // assign to threads
                    if (HttpContext.Current != null)
                    {
                        HttpContext.Current.User = claimsPrincipal;
                    }
                    Thread.CurrentPrincipal = claimsPrincipal;
                }
            }
            catch (InvalidTokenReceivedException ex)
            {
                error = new ResourceAccessErrorResponse(_realm, ex.ErrorCode, ex.ErrorDescription);
            }
            catch (ExpiredTokenReceivedException ex)
            {
                error = new ResourceAccessErrorResponse(_realm, ex.ErrorCode, ex.ErrorDescription);
            }
            catch (Exception)
            {
                error = new ResourceAccessErrorResponse(_realm, "SWT401", "Token validation failed");
            }

            return tokenValid;
        }