Exemplo n.º 1
0
        protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
        {
            AuthenticateResult authResult = await HandleAuthenticateOnceSafeAsync();

            HmacChallengeContext eventContext = new HmacChallengeContext(Context, Scheme, Options, properties)
            {
                AuthenticationFailure = authResult?.Failure
            };

            if (Options.IncludeErrorDetails && eventContext.AuthenticationFailure != null)
            {
                eventContext.Error            = "invalid_key";
                eventContext.ErrorDescription = CreateErrorDescription(eventContext.AuthenticationFailure);
            }

            await Events.Challenge(eventContext);

            if (eventContext.Handled)
            {
                return;
            }

            Response.StatusCode = 401;

            if (string.IsNullOrEmpty(eventContext.Error) &&
                string.IsNullOrEmpty(eventContext.ErrorDescription) &&
                string.IsNullOrEmpty(eventContext.ErrorUri))
            {
                Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.WWWAuthenticate, Options.Challenge);
            }
            else
            {
                StringBuilder builder = new StringBuilder(Options.Challenge);
                if (Options.Challenge.IndexOf(" ", StringComparison.Ordinal) > 0)
                {
                    builder.Append(",");
                }
                if (!string.IsNullOrEmpty(eventContext.Error))
                {
                    builder.Append(" error=\"");
                    builder.Append(eventContext.Error);
                    builder.Append("\"");
                }
                if (!string.IsNullOrEmpty(eventContext.ErrorDescription))
                {
                    if (!string.IsNullOrEmpty(eventContext.Error))
                    {
                        builder.Append(",");
                    }

                    builder.Append(" error_description=\"");
                    builder.Append(eventContext.ErrorDescription);
                    builder.Append("\"");
                }
                if (!string.IsNullOrEmpty(eventContext.ErrorUri))
                {
                    if (!string.IsNullOrEmpty(eventContext.Error) ||
                        !string.IsNullOrEmpty(eventContext.ErrorDescription))
                    {
                        builder.Append(",");
                    }

                    builder.Append(" error_uri=\"");
                    builder.Append(eventContext.ErrorUri);
                    builder.Append("\"");
                }

                Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.WWWAuthenticate, builder.ToString());
            }
        }
Exemplo n.º 2
0
 public virtual Task Challenge(HmacChallengeContext context) => OnChallenge(context);