internal static AuthenticationChallenge Parse(string value) { AuthenticationChallenge authenticationChallenge; AuthenticationChallenge authenticationChallenge1; string[] strArrays = value.Split(new char[] { ' ' }, 2); if ((int)strArrays.Length == 2) { string lower = strArrays[0].ToLower(); if (lower == "basic") { authenticationChallenge1 = new AuthenticationChallenge(AuthenticationSchemes.Basic, AuthenticationBase.ParseParameters(strArrays[1])); } else if (lower == "digest") { authenticationChallenge1 = new AuthenticationChallenge(AuthenticationSchemes.Digest, AuthenticationBase.ParseParameters(strArrays[1])); } else { authenticationChallenge1 = null; } authenticationChallenge = authenticationChallenge1; } else { authenticationChallenge = null; } return(authenticationChallenge); }
internal void SendAuthenticationChallenge( AuthenticationSchemes scheme, string realm ) { var chal = new AuthenticationChallenge(scheme, realm).ToString(); _response.StatusCode = 401; _response.Headers.InternalSet("WWW-Authenticate", chal, true); _response.Close(); }
internal bool Authenticate() { var schm = _listener.SelectAuthenticationScheme(_request); if (schm == AuthenticationSchemes.Anonymous) { return(true); } var basicAllowed = HasFlag(schm, AuthenticationSchemes.Basic); var digestAllowed = HasFlag(schm, AuthenticationSchemes.Digest); if (!basicAllowed && !digestAllowed) { _response.Close(HttpStatusCode.Forbidden); return(false); } var realm = _listener.GetRealm(); if (basicAllowed) { var user = HttpUtility.CreateUser(_request.Headers["Authorization"], AuthenticationSchemes.Basic, realm, _request.HttpMethod, _listener.GetUserCredentialsFinder()); if (user?.Identity?.IsAuthenticated == true) { _user = user; return(true); } } if (digestAllowed) { var user = HttpUtility.CreateUser(_request.Headers["Authorization"], AuthenticationSchemes.Digest, realm, _request.HttpMethod, _listener.GetUserCredentialsFinder()); if (user?.Identity?.IsAuthenticated == true) { _user = user; return(true); } } if (!digestAllowed) { _response.CloseWithAuthChallenge(AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()); } else { _response.CloseWithAuthChallenge(AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()); } return(false); }
internal void Complete(HttpListenerContext context, bool syncCompleted) { var listener = context.Listener; var scheme = listener.SelectAuthenticationScheme(context); if (scheme == AuthenticationSchemes.None) { context.Response.Close(HttpStatusCode.Forbidden); listener.BeginGetContext(this); return; } var header = context.Request.Headers ["Authorization"]; if (scheme == AuthenticationSchemes.Basic && (header == null || !header.StartsWith("basic", StringComparison.OrdinalIgnoreCase))) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateBasicChallenge(listener.Realm).ToBasicString()); listener.BeginGetContext(this); return; } if (scheme == AuthenticationSchemes.Digest && (header == null || !header.StartsWith("digest", StringComparison.OrdinalIgnoreCase))) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateDigestChallenge(listener.Realm).ToDigestString()); listener.BeginGetContext(this); return; } _context = context; _syncCompleted = syncCompleted; lock (_sync) { _completed = true; if (_waitHandle != null) { _waitHandle.Set(); } if (_callback != null) { ThreadPool.QueueUserWorkItem(invokeCallback, this); } } }
internal bool Authenticate(HttpListenerContext context) { var schm = SelectAuthenticationScheme(context); if (schm == AuthenticationSchemes.Anonymous) { return(true); } if (schm != AuthenticationSchemes.Basic && schm != AuthenticationSchemes.Digest) { context.Response.Close(HttpStatusCode.Forbidden); return(false); } var realm = Realm; var req = context.Request; var user = HttpUtility.CreateUser( req.Headers["Authorization"], schm, realm, req.HttpMethod, UserCredentialsFinder); if (user != null && user.Identity.IsAuthenticated) { context.User = user; return(true); } if (schm == AuthenticationSchemes.Basic) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()); } if (schm == AuthenticationSchemes.Digest) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()); } return(false); }
internal bool Authenticate() { var schm = _listener.SelectAuthenticationScheme(_request); if (schm == AuthenticationSchemes.Anonymous) { return(true); } if (schm == AuthenticationSchemes.None) { _errorStatusCode = 403; _errorMessage = "Authentication not allowed"; SendError(); return(false); } var realm = _listener.GetRealm(); var user = HttpUtility.CreateUser( _request.Headers["Authorization"], schm, realm, _request.HttpMethod, _listener.GetUserCredentialsFinder() ); if (user == null || !user.Identity.IsAuthenticated) { var chal = new AuthenticationChallenge(schm, realm).ToString(); sendAuthenticationChallenge(chal); return(false); } _user = user; return(true); }
internal bool Authenticate(HttpListenerContext context) { AuthenticationSchemes authenticationSchemes = SelectAuthenticationScheme(context); switch (authenticationSchemes) { case AuthenticationSchemes.Anonymous: return(true); default: context.Response.Close(HttpStatusCode.Forbidden); return(false); case AuthenticationSchemes.Digest: case AuthenticationSchemes.Basic: { string realm = Realm; HttpListenerRequest request = context.Request; IPrincipal principal = HttpUtility.CreateUser(request.Headers["Authorization"], authenticationSchemes, realm, request.HttpMethod, UserCredentialsFinder); if (principal != null && principal.Identity.IsAuthenticated) { context.User = principal; return(true); } if (authenticationSchemes == AuthenticationSchemes.Basic) { context.Response.CloseWithAuthChallenge(AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()); } if (authenticationSchemes == AuthenticationSchemes.Digest) { context.Response.CloseWithAuthChallenge(AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()); } return(false); } } }
private static bool authenticate( HttpListenerContext context, AuthenticationSchemes scheme, string realm, Func <IIdentity, NetworkCredential> credentialsFinder) { if (!(scheme == AuthenticationSchemes.Basic || scheme == AuthenticationSchemes.Digest)) { context.Response.Close(HttpStatusCode.Forbidden); return(false); } var req = context.Request; var user = HttpUtility.CreateUser( req.Headers["Authorization"], scheme, realm, req.HttpMethod, credentialsFinder); if (user != null && user.Identity.IsAuthenticated) { context.User = user; return(true); } if (scheme == AuthenticationSchemes.Basic) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()); } if (scheme == AuthenticationSchemes.Digest) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()); } return(false); }
internal AuthenticationResponse( AuthenticationChallenge challenge, NetworkCredential credentials, uint nonceCount) : this(challenge.Scheme, challenge.Parameters, credentials, nonceCount) { }
internal AuthenticationResponse ( AuthenticationChallenge challenge, NetworkCredential credentials, uint nonceCount) : this (challenge.Scheme, challenge.Parameters, credentials, nonceCount) { }
private static bool authenticate(HttpListenerContext context) { var listener = context.Listener; var schm = listener.SelectAuthenticationScheme(context); if (schm == AuthenticationSchemes.Anonymous) { return(true); } var req = context.Request; if (schm == AuthenticationSchemes.Basic) { var authRes = req.Headers["Authorization"]; if (authRes == null || !authRes.StartsWith("basic", StringComparison.OrdinalIgnoreCase)) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateBasicChallenge(listener.Realm).ToBasicString()); return(false); } } else if (schm == AuthenticationSchemes.Digest) { var authRes = req.Headers["Authorization"]; if (authRes == null || !authRes.StartsWith("digest", StringComparison.OrdinalIgnoreCase)) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateDigestChallenge(listener.Realm).ToDigestString()); return(false); } } else { context.Response.Close(HttpStatusCode.Forbidden); return(false); } var realm = listener.Realm; context.SetUser(schm, realm, listener.UserCredentialsFinder); if (req.IsAuthenticated) { return(true); } if (schm == AuthenticationSchemes.Basic) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()); } if (schm == AuthenticationSchemes.Digest) { context.Response.CloseWithAuthChallenge( AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()); } return(false); }