Esempio n. 1
0
        private Task SendErrorAsJsonAsync(BaseValidatingContext <OAuthAuthorizationServerOptions> validatingContext)
        {
            string error            = validatingContext.HasError ? validatingContext.Error : Constants.Errors.InvalidRequest;
            string errorDescription = validatingContext.HasError ? validatingContext.ErrorDescription : null;
            string errorUri         = validatingContext.HasError ? validatingContext.ErrorUri : null;

            string body;

            MemoryStream stream, memoryStream = null;

            StreamWriter streamWriter = null;

            try
            {
                stream = memoryStream = new MemoryStream();

                streamWriter = new StreamWriter(memoryStream);

                using (var writer = new JsonTextWriter(streamWriter))
                {
                    memoryStream = null;

                    streamWriter = null;

                    writer.WriteStartObject();
                    writer.WritePropertyName(Constants.Parameters.Error);
                    writer.WriteValue(error);
                    if (!string.IsNullOrEmpty(errorDescription))
                    {
                        writer.WritePropertyName(Constants.Parameters.ErrorDescription);
                        writer.WriteValue(errorDescription);
                    }
                    if (!string.IsNullOrEmpty(errorUri))
                    {
                        writer.WritePropertyName(Constants.Parameters.ErrorUri);
                        writer.WriteValue(errorUri);
                    }
                    writer.WriteEndObject();
                    writer.Flush();
                    body = Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }

            Response.StatusCode  = 400;
            Response.ContentType = "application/json;charset=UTF-8";
            Response.Headers["Cache-Control"]  = "no-cache";
            Response.Headers["Pragma"]         = "no-cache";
            Response.Headers["Expires"]        = "-1";
            Response.Headers["Content-Length"] = body.Length.ToString(CultureInfo.InvariantCulture);
            return(Response.WriteAsync(body, Context.RequestAborted));
        }
Esempio n. 2
0
        private static AuthenticationTicket ReturnOutcome(OAuthValidateTokenRequestContext validatingContext, BaseValidatingContext <OAuthAuthorizationServerOptions> grantContext, AuthenticationTicket ticket, string defaultError)
        {
            if (!validatingContext.IsValidated)
            {
                return(null);
            }

            if (!grantContext.IsValidated)
            {
                if (grantContext.HasError)
                {
                    validatingContext.SetError(grantContext.Error, grantContext.ErrorDescription, grantContext.ErrorUri);
                }
                else
                {
                    validatingContext.SetError(defaultError);
                }

                return(null);
            }

            if (ticket == null)
            {
                validatingContext.SetError(defaultError);
                return(null);
            }

            return(ticket);
        }
Esempio n. 3
0
        private Task <bool> SendErrorRedirectAsync(OAuthValidateClientRedirectUriContext clientContext, BaseValidatingContext <OAuthAuthorizationServerOptions> validatingContext)
        {
            if (clientContext == null)
            {
                throw new ArgumentNullException("clientContext");
            }

            string error            = validatingContext.HasError ? validatingContext.Error : Constants.Errors.InvalidRequest;
            string errorDescription = validatingContext.HasError ? validatingContext.ErrorDescription : null;
            string errorUri         = validatingContext.HasError ? validatingContext.ErrorUri : null;

            if (!clientContext.IsValidated)
            {
                // write error in response body if client_id or redirect_uri have not been validated
                return(SendErrorPageAsync(error, errorDescription, errorUri));
            }

            // redirect with error if client_id and redirect_uri have been validated
            string location = QueryHelpers.AddQueryString(clientContext.RedirectUri, Constants.Parameters.Error, error);

            if (!string.IsNullOrEmpty(errorDescription))
            {
                location = QueryHelpers.AddQueryString(location, Constants.Parameters.ErrorDescription, errorDescription);
            }
            if (!string.IsNullOrEmpty(errorUri))
            {
                location = QueryHelpers.AddQueryString(location, Constants.Parameters.ErrorUri, errorUri);
            }
            Response.Redirect(location);
            // request is handled, does not pass on to application
            return(Task.FromResult(true));
        }