public static BasicAuthenticationCredential Extract(string header)
        {
            if (string.IsNullOrEmpty(header)) throw new ArgumentNullException(nameof(header));

            var credential = new BasicAuthenticationCredential
            {
                Header = header
            };

            string pair;
            try
            {
                pair = Encoding.UTF8.GetString(
                    Convert.FromBase64String(header.Substring("Basic ".Length)));
            }
            catch (FormatException ex)
            {
                throw new MalformedCredentialException(credential, ex);
            }
            catch (ArgumentException ex)
            {
                throw new MalformedCredentialException(credential, ex);
            }

            var ix = pair.IndexOf(':');
            if (ix == -1)
            {
                throw new MalformedCredentialException(credential);
            }

            credential.Id = pair.Substring(0, ix);
            credential.Secret = pair.Substring(ix + 1);

            return credential;
        }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            string[] authHeader;

            if (Request.Headers.TryGetValue("Authorization", out authHeader) &&
                authHeader.Any() &&
                authHeader.First().StartsWith("Basic "))
            {
                try
                {
                    var credential = BasicAuthenticationCredential.Extract(authHeader.FirstOrDefault());

                    var credentialReceicedNotification = new CredentialReceivedNotification <BasicAuthenticationOptions>(Context, Options)
                    {
                        Credential           = credential,
                        AuthenticationTicket = new AuthenticationTicket(new AuthenticationProperties(), Options.AuthenticationScheme)
                    };

                    await Options.Notifications.CredentialReceived(credentialReceicedNotification);

                    return(credentialReceicedNotification.AuthenticationTicket);
                }
                catch (Exception ex)
                {
                    var authenticationFailedNotification =
                        new AuthenticationFailedNotification <HttpContext, BasicAuthenticationOptions>(Context, Options)
                    {
                        ProtocolMessage = Context,
                        Exception       = ex
                    };

                    await Options.Notifications.AuthenticationFailed(authenticationFailedNotification);

                    if (authenticationFailedNotification.HandledResponse)
                    {
                        return(authenticationFailedNotification.AuthenticationTicket);
                    }

                    if (authenticationFailedNotification.Skipped)
                    {
                        return(null);
                    }

                    throw;
                }
            }
            else
            {
                return(null);
            }
        }
        public static BasicAuthenticationCredential Extract(string header)
        {
            if (string.IsNullOrEmpty(header))
            {
                throw new ArgumentNullException(nameof(header));
            }

            var credential = new BasicAuthenticationCredential
            {
                Header = header
            };

            string pair;

            try
            {
                pair = Encoding.UTF8.GetString(
                    Convert.FromBase64String(header.Substring("Basic ".Length)));
            }
            catch (FormatException ex)
            {
                throw new MalformedCredentialException(credential, ex);
            }
            catch (ArgumentException ex)
            {
                throw new MalformedCredentialException(credential, ex);
            }

            var ix = pair.IndexOf(':');

            if (ix == -1)
            {
                throw new MalformedCredentialException(credential);
            }

            credential.Id     = pair.Substring(0, ix);
            credential.Secret = pair.Substring(ix + 1);

            return(credential);
        }
 public MalformedCredentialException(BasicAuthenticationCredential credential, Exception inner) : base("Malformed credential", inner) { _credential = credential; }
Ejemplo n.º 5
0
 public MalformedCredentialException(BasicAuthenticationCredential credential, Exception inner) : base("Malformed credential", inner)
 {
     _credential = credential;
 }