public override Task RequestToken(OAuthRequestTokenContext context) {
            if (!String.IsNullOrEmpty(context.Token))
                return Task.FromResult(0);

            context.Token = context.Request.Query.Get("access_token");
            if (!String.IsNullOrEmpty(context.Token))
                return Task.FromResult(0);


            var authHeader = context.Request.Headers.Get("Authorization");
            if (String.IsNullOrEmpty(authHeader))
                return Task.FromResult(0);

            var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
            if (authHeaderVal.Scheme.Equals("token", StringComparison.OrdinalIgnoreCase)) {
                context.Token = authHeaderVal.Parameter;
            } else if (authHeaderVal.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase)) {
                string text = Encoding.UTF8.GetString(Convert.FromBase64String(authHeaderVal.Parameter));
                int delimiterIndex = text.IndexOf(':');
                if (delimiterIndex >= 0 && String.Equals("client", text.Substring(0, delimiterIndex), StringComparison.OrdinalIgnoreCase))
                    context.Token = text.Substring(delimiterIndex + 1);
            }

            return Task.FromResult(0);
        }
        public override async Task RequestToken(OAuthRequestTokenContext context)
        {
            var value = context.Request.Query.Get(_name);

            if (!string.IsNullOrEmpty(value))
            {
                context.Token = value;
            }
        }
 public override Task RequestToken(OAuthRequestTokenContext context)
 {
     var token = context.OwinContext.Request.Cookies["BearerToken"];
     if (!string.IsNullOrWhiteSpace(token))
     {
         context.Token = token;
     }
     return Task.FromResult<object>(null);
 }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var value = context.Request.Query.Get(_name);

            if (!string.IsNullOrEmpty(value))
                context.Token = value;

            return Task.FromResult<object>(null);
        }
Ejemplo n.º 5
0
 public override Task RequestToken(OAuthRequestTokenContext context)
 {
     var getter = locations.FirstOrDefault(x => !String.IsNullOrWhiteSpace(x(context.Request)));
     if (getter != null)
     {
         var tokenStr = getter(context.Request);
         context.Token = bearerRegex.Replace(tokenStr, "").Trim();
     }
     return Task.FromResult<object>(null);
 }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var tokenCookie = context.OwinContext.Request.Cookies["BearerToken"];
            if (!string.IsNullOrEmpty(tokenCookie))
            {
                context.Token = tokenCookie;
                return Task.FromResult<object>(null);
            }

            return base.RequestToken(context);
        }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            // try to find bearer token in a cookie 
            // (by default OAuthBearerAuthenticationHandler 
            // only checks Authorization header)
            var tokenCookie = context.OwinContext.Request.Cookies["BearerToken"];
            if (!string.IsNullOrEmpty(tokenCookie))
                context.Token = tokenCookie;
            return Task.FromResult<object>(null);
        }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var value = context.Request.Headers["Authorization"].Substring("Bearer ".Length).Trim() 
                ?? context.Request.Query.Get("access_token");

            if (!string.IsNullOrEmpty(value))
            {
                context.Token = value;
            }

            return Task.FromResult<object>(null);
        }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var queryStringToken = context.Request.Query.Get("bearer_token");
            if (string.IsNullOrWhiteSpace(queryStringToken))
            {
                return Task.FromResult<object>(null);
            }

            var regex = new Regex("bearer");
            context.Token = regex.Replace(queryStringToken, string.Empty).Trim();

            return Task.FromResult<object>(null);
        }
 private void applyAuthorizationClientApi(OAuthRequestTokenContext context)
 {
     string input = context.Request.Headers.Get("Authorization");
     if (string.IsNullOrEmpty(input))
         return;
     AuthenticationHeaderValue authenticationHeaderValue = AuthenticationHeaderValue.Parse(input);
     if (authenticationHeaderValue == null || authenticationHeaderValue.Scheme != "Cliamx")
         return;
     string result = ClientApiProvider.GetAccessToken(authenticationHeaderValue, context.OwinContext.Request).Result;
     if (string.IsNullOrEmpty(result))
         return;
     string str = new AuthenticationHeaderValue("Bearer", result).ToString();
     context.Request.Headers.Set("Authorization", str);
     context.Token = result;
 }
 public override Task RequestToken(OAuthRequestTokenContext context)
 {
     try
     {
         IHeaderDictionary headers = context.Request.Headers;
         string input = headers.Get("Authorization");
         if (!string.IsNullOrEmpty(input))
         {
             if (input.IndexOf(' ') == -1)
                 input = string.Format("{0} {1}", "Cliamx", input);
             AuthenticationHeaderValue authenticationHeaderValue = AuthenticationHeaderValue.Parse(input);
             if (authenticationHeaderValue != null && authenticationHeaderValue.Scheme == "Cliamx")
             {
                 string result = ClientApiProvider.GetAccessToken(authenticationHeaderValue, context.OwinContext.Request).Result;
                 if (!string.IsNullOrEmpty(result))
                 {
                     string str = new AuthenticationHeaderValue("Bearer", result).ToString();
                     context.Request.Headers.Set("Authorization", str);
                     context.Token = result;
                 }
             }
             else if (authenticationHeaderValue != null)
             {
                 if (authenticationHeaderValue.Scheme == "Bearer")
                 {
                     string username = headers.Get("userName");
                     string realAccessToken;
                     if (AppAuthorizeAttribute.ValidateOAuthAuthorizationHeader(username, authenticationHeaderValue.Parameter, out realAccessToken))
                     {
                         string str = new AuthenticationHeaderValue(authenticationHeaderValue.Scheme, realAccessToken).ToString();
                         context.Request.Headers.Set("Authorization", str);
                         context.Token = realAccessToken;
                     }
                     else
                     {
                         context.Request.Headers.Remove("Authorization");
                         context.Token = null;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Log.Logger.Error("RequestToken" + ex.ToString());
     }
     return base.RequestToken(context);
 }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var qvalue = context.Request.Query.Get(_name);
            var hvalue = context.Request.Headers["Authorization"];

            if (!string.IsNullOrEmpty(qvalue))
            {
                context.Token = qvalue;
            }
            else if (!string.IsNullOrEmpty(hvalue))
            {
                context.Token = hvalue;
            }

            return Task.FromResult<object>(null);
        }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var value = context.Request.Headers.Get("Authorization");

            if (string.IsNullOrEmpty(value))
            {
                value = context.Request.Query.Get("token");
            }

            if (!string.IsNullOrEmpty(value))
            {
                context.Token = value;
            }

            return Task.FromResult<object>(null);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Handles processing OAuth bearer token.
        /// First tries to find the bearer token in then query string parameter.
        /// If it is not in the query string parameter, fall back to the default header parameter.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task RequestToken(OAuthRequestTokenContext context)
        {
            // Get the token from the query string parameter

            var value = context.Request.Query.Get(_name);

            if (!String.IsNullOrEmpty(value))
            {
                context.Token = value;
            }

            // If not in query string parameter fall back to default

            if (String.IsNullOrEmpty(value))
            {
                await base.RequestToken(context);
            }
        }
        public override Task RequestToken(OAuthRequestTokenContext context) {
            if (!String.IsNullOrEmpty(context.Token))
                return Task.FromResult(0);

            context.Token = context.Request.Query.Get("access_token");
            if (!String.IsNullOrEmpty(context.Token))
                return Task.FromResult(0);


            var authHeader = context.Request.Headers.Get("Authorization");
            if (String.IsNullOrEmpty(authHeader))
                return Task.FromResult(0);

            var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
            if (authHeaderVal.Scheme.Equals("token", StringComparison.OrdinalIgnoreCase))
                context.Token = authHeaderVal.Parameter;

            return Task.FromResult(0);
        }
 //<summary>
 //验证Token
 //</summary>
 //<param name="context">上下文</param>
 //<returns></returns>
 public override async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
 {
     var request = new OAuthRequestTokenContext(context.OwinContext, context.Token);
     var ticket = new AuthenticationTicket(new ClaimsIdentity(), new AuthenticationProperties()
     {
         IssuedUtc = DateTime.UtcNow.AddYears(-1),
         ExpiresUtc = DateTime.UtcNow.AddYears(-1)
     });
     if (request == null || request.Token.IsNullOrEmpty())
     {
         context.SetTicket(ticket);
     }
     //验证Token是否过期
     var vaild = true;//await _clientAuthorizationService.VaildOAuthClientSecretAsync();
     if (vaild)
     {
         context.SetTicket(ticket);
     }
 }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var headers = context.Request.Headers;
            var authorization = headers.Get(ClientApiProvider.ClientHeaderAuthName);
            if (!string.IsNullOrEmpty(authorization))
            {
                var authentication = AuthenticationHeaderValue.Parse(authorization);
                if (authentication != null && authentication.Scheme == ClientApiProvider.ClientHeaderAuthScheme)
                {
                    string accessToken = ClientApiProvider.GetAccessToken(authentication, context.OwinContext.Request).Result;
                    if (!string.IsNullOrEmpty(accessToken))
                    {
                        var authorizationHeader = new AuthenticationHeaderValue(ClientApiProvider.OAuthHeaderScheme, accessToken).ToString();
                        context.Request.Headers.Set(ClientApiProvider.OAuthHeaderName, authorizationHeader);
                        context.Token = accessToken;
                    }
                }
            }

            return base.RequestToken(context);
        }
 private void applyAuthorizationClientOAuth(OAuthRequestTokenContext context)
 {
     IHeaderDictionary headers = context.Request.Headers;
     string input = headers.Get("Authorization");
     if (string.IsNullOrEmpty(input))
         return;
     AuthenticationHeaderValue authenticationHeaderValue = AuthenticationHeaderValue.Parse(input);
     if (authenticationHeaderValue == null || authenticationHeaderValue.Scheme != "Bearer")
         return;
     string username = headers.Get("userName");
     string realAccessToken = (string)null;
     if (AppAuthorizeAttribute.ValidateOAuthAuthorizationHeader(username, authenticationHeaderValue.Parameter, out realAccessToken))
     {
         string str = new AuthenticationHeaderValue(authenticationHeaderValue.Scheme, realAccessToken).ToString();
         context.Request.Headers.Set("Authorization", str);
         context.Token = realAccessToken;
     }
     else
     {
         context.Request.Headers.Remove("Authorization");
         context.Token = (string)null;
     }
 }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            if (string.IsNullOrEmpty(context.Token))
            {
                // try to find bearer token in a cookie
                var tokenCookie = context.OwinContext.Request.Cookies["authentication"];
                if (!string.IsNullOrEmpty(tokenCookie))
                {
                    context.Token = tokenCookie;
                }
                else
                {
                    // try to find bearer token in the standard Authorization header - should not occur for the web app.
                    var tokenHeader = context.OwinContext.Request.Headers["Authorization"];
                    if (!string.IsNullOrEmpty(tokenHeader))
                    {
                        context.Token = tokenHeader;
                    }
                }
            }

            return Task.FromResult<object>(null);
        }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            try
            {
                // Find token in default location
                string requestToken  = null;
                string authorization = Request.Headers.Get("Authorization");
                if (!string.IsNullOrEmpty(authorization))
                {
                    if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
                    {
                        requestToken = authorization.Substring("Bearer ".Length).Trim();
                    }
                }

                // Give application opportunity to find from a different location, adjust, or reject token
                var requestTokenContext = new OAuthRequestTokenContext(Context, requestToken);
                await Options.Provider.RequestToken(requestTokenContext);

                // If no token found, no further work possible
                if (string.IsNullOrEmpty(requestTokenContext.Token))
                {
                    return(null);
                }

                // Call provider to process the token into data
                var tokenReceiveContext = new AuthenticationTokenReceiveContext(
                    Context,
                    Options.AccessTokenFormat,
                    requestTokenContext.Token);

                await Options.AccessTokenProvider.ReceiveAsync(tokenReceiveContext);

                if (tokenReceiveContext.Ticket == null)
                {
                    tokenReceiveContext.DeserializeTicket(tokenReceiveContext.Token);
                }

                AuthenticationTicket ticket = tokenReceiveContext.Ticket;
                if (ticket == null)
                {
                    _logger.WriteWarning("invalid bearer token received");
                    return(null);
                }

                // Validate expiration time if present
                DateTimeOffset currentUtc = Options.SystemClock.UtcNow;

                if (ticket.Properties.ExpiresUtc.HasValue &&
                    ticket.Properties.ExpiresUtc.Value < currentUtc)
                {
                    _logger.WriteWarning("expired bearer token received");
                    return(null);
                }

                // Give application final opportunity to override results
                var context = new OAuthValidateIdentityContext(Context, Options, ticket);
                if (ticket != null &&
                    ticket.Identity != null &&
                    ticket.Identity.IsAuthenticated)
                {
                    // bearer token with identity starts validated
                    context.Validated();
                }
                if (Options.Provider != null)
                {
                    await Options.Provider.ValidateIdentity(context);
                }
                if (!context.IsValidated)
                {
                    return(null);
                }

                // resulting identity values go back to caller
                return(context.Ticket);
            }
            catch (Exception ex)
            {
                _logger.WriteError("Authentication failed", ex);
                return(null);
            }
        }
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            context.Token = context.Request.Query.Get(_name);

            return Task.FromResult<object>(null);
        }
 /// <summary>
 /// Handles processing OAuth bearer token.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual Task RequestToken(OAuthRequestTokenContext context)
 {
     return(OnRequestToken(context));
 }
        private async Task<string> GetTokenAsync(OwinContext context)
        {
            // find token in default location
            string requestToken = null;
            string authorization = context.Request.Headers.Get("Authorization");
            if (!string.IsNullOrEmpty(authorization))
            {
                if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
                {
                    requestToken = authorization.Substring("Bearer ".Length).Trim();
                }
            }

            // give application opportunity to find from a different location, adjust, or reject token
            if (_options.TokenProvider != null)
            {
                var requestTokenContext = new OAuthRequestTokenContext(context, requestToken);
                await _options.TokenProvider.RequestToken(requestTokenContext);

                // if no token found, no further work possible
                if (string.IsNullOrEmpty(requestTokenContext.Token))
                {
                    return null;
                }

                return requestTokenContext.Token;
            }

            return requestToken;
        }
 /// <summary>
 /// Handles processing OAuth bearer token.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual Task RequestToken(OAuthRequestTokenContext context)
 {
     return OnRequestToken(context);
 }
Ejemplo n.º 25
0
 public Task RequestToken(OAuthRequestTokenContext context)
 {
     return Task.FromResult<object>(null);
 }
 public override System.Threading.Tasks.Task RequestToken(OAuthRequestTokenContext context)
 {
     return base.RequestToken(context);
 }
 /// <summary>
 /// Invoked before the <see cref="T:System.Security.Claims.ClaimsIdentity" /> is created. Gives the application an
 /// opportunity to find the identity from a different location, adjust, or reject the token.
 /// </summary>
 /// <param name="context">Contains the token string.</param>
 /// <returns>
 /// A <see cref="T:System.Threading.Tasks.Task" /> representing the completed operation.
 /// </returns>
 public virtual Task RequestToken(OAuthRequestTokenContext context)
 {
     context.Token = context.OwinContext.Get<string>("idsrv:tokenvalidation:token");
     return Task.FromResult(0);
 }