internal IPrincipal ParseBasicAuthentication(string authData) { try { // Basic AUTH Data is a formatted Base64 String //string domain = null; var authString = Encoding.GetEncoding(0).GetString(Convert.FromBase64String(authData)); // The format is DOMAIN\username:password // Domain is optional var pos = authString.IndexOf(':'); // parse the password off the end var password = authString.Substring(pos + 1); // discard the password authString = authString.Substring(0, pos); // check if there is a domain pos = authString.IndexOf('\\'); var user = pos > 0 ? authString.Substring(pos) : authString; var identity = new HttpListenerBasicIdentity(user, password); // TODO: What are the roles MS sets return(new GenericPrincipal(identity, new string[0])); } catch (Exception) { // Invalid auth data is swallowed silently return(null); } }
internal IPrincipal ParseBasicAuthentication(string authData) { IPrincipal result; try { string text = Encoding.Default.GetString(Convert.FromBase64String(authData)); int num = text.IndexOf(':'); string password = text.Substring(num + 1); text = text.Substring(0, num); num = text.IndexOf('\\'); string username; if (num > 0) { username = text.Substring(num); } else { username = text; } HttpListenerBasicIdentity identity = new HttpListenerBasicIdentity(username, password); result = new GenericPrincipal(identity, new string[0]); } catch (Exception) { result = null; } return(result); }
internal IPrincipal ParseBasicAuthentication(string authData) { try { // Basic AUTH Data is a formatted Base64 String //string domain = null; string user = null; string password = null; int pos = -1; string authString = System.Text.Encoding.Default.GetString(Convert.FromBase64String(authData)); // The format is DOMAIN\username:password // Domain is optional pos = authString.IndexOf(':'); // parse the password off the end password = authString.Substring(pos + 1); // discard the password authString = authString.Substring(0, pos); // check if there is a domain pos = authString.IndexOf('\\'); if (pos > 0) { //domain = authString.Substring (0, pos); user = authString.Substring(pos); } else { user = authString; } HttpListenerBasicIdentity identity = new HttpListenerBasicIdentity(user, password); // TODO: What are the roles MS sets return(new GenericPrincipal(identity, new string [0])); } catch (Exception) { // Invalid auth data is swallowed silently return(null); } }
public bool TryGetBasicAuthenticationCredentialsFromRequest(HttpRequestMessage request, out HttpListenerBasicIdentity identity) { identity = null; var header = request.Headers.Authorization; if (header != null && header.Scheme.Equals("Basic")) { string encodedUserPass = header.Parameter; Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string userPass = encoding.GetString(Convert.FromBase64String(encodedUserPass)); int separator = userPass.IndexOf(':'); string[] credentials = new string[2]; credentials[0] = userPass.Substring(0, separator); credentials[1] = userPass.Substring(separator + 1); identity = new HttpListenerBasicIdentity(credentials[0], credentials[1]); return true; } return false; }
public bool TryGetBasicAuthenticationCredentialsFromRequest(HttpRequestBase request, out HttpListenerBasicIdentity identity) { identity = null; string header = request.Headers["Authorization"] ?? request.Headers["X-Authorization"]; if (header != null && header.StartsWith("Basic")) { string encodedUserPass = header.Substring(6).Trim(); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string userPass = encoding.GetString(Convert.FromBase64String(encodedUserPass)); int separator = userPass.IndexOf(':'); string[] credentials = new string[2]; credentials[0] = userPass.Substring(0, separator); credentials[1] = userPass.Substring(separator + 1); identity = new HttpListenerBasicIdentity(credentials[0], credentials[1]); return true; } return false; }
internal IPrincipal ParseBasicAuthentication (string authData) { try { // Basic AUTH Data is a formatted Base64 String //string domain = null; string user = null; string password = null; int pos = -1; string authString = System.Text.Encoding.Default.GetString (Convert.FromBase64String (authData)); // The format is DOMAIN\username:password // Domain is optional pos = authString.IndexOf (':'); // parse the password off the end password = authString.Substring (pos+1); // discard the password authString = authString.Substring (0, pos); // check if there is a domain pos = authString.IndexOf ('\\'); if (pos > 0) { //domain = authString.Substring (0, pos); user = authString.Substring (pos); } else { user = authString; } HttpListenerBasicIdentity identity = new HttpListenerBasicIdentity (user, password); // TODO: What are the roles MS sets return new GenericPrincipal (identity, new string [0]); } catch (Exception) { // Invalid auth data is swallowed silently return null; } }
public bool TryGetUserNameCredentialsFromWrapRequest(HttpRequestBase request, out HttpListenerBasicIdentity identity) { identity = null; var userName = request.Form["wrap_name"]; var password = request.Form["wrap_password"]; if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password)) { return false; } identity = new HttpListenerBasicIdentity(userName, password); return true; }
private static void Authenticate(HttpListenerContext context, HttpListenerBasicIdentity identity) { if (Username != identity.Name || Password != identity.Password) { context.Response.AddHeader("WWW-Authenticate", "Basic Realm=\"" + HttpListener.Realm + "\""); context.Response.StatusCode = 401; context.Response.Close(); throw new UnauthorizedAccessException(); } }
public void Basic4 () { HttpListenerBasicIdentity bi = new HttpListenerBasicIdentity ("hey", "pass"); Assert.AreEqual ("Basic", bi.AuthenticationType, "#01"); Assert.AreEqual ("hey", bi.Name, "#02"); Assert.IsTrue (bi.IsAuthenticated, "#03"); Assert.AreEqual ("pass", bi.Password, "#04"); }
public void Basic2 () { HttpListenerBasicIdentity bi = new HttpListenerBasicIdentity ("", null); Assert.AreEqual ("Basic", bi.AuthenticationType, "#01"); Assert.AreEqual ("", bi.Name, "#02"); Assert.IsFalse (bi.IsAuthenticated, "#03"); Assert.IsNull (bi.Password, "#04"); }
public void Basic1 () { HttpListenerBasicIdentity bi = new HttpListenerBasicIdentity (null, null); }