internal static void FixNetworkCredential(ref NetworkCredential credential) { if (credential == null) { return; } string username = NetworkCredentialHelper.UnsafeGetUsername(credential); string domain = NetworkCredentialHelper.UnsafeGetDomain(credential); if (!string.IsNullOrEmpty(username) && string.IsNullOrEmpty(domain)) { // do the splitting only if there is exactly 1 \ or exactly 1 @ string[] partsWithSlashDelimiter = username.Split('\\'); string[] partsWithAtDelimiter = username.Split('@'); if (partsWithSlashDelimiter.Length == 2 && partsWithAtDelimiter.Length == 1) { if (!string.IsNullOrEmpty(partsWithSlashDelimiter[0]) && !string.IsNullOrEmpty(partsWithSlashDelimiter[1])) { credential = new NetworkCredential(partsWithSlashDelimiter[1], NetworkCredentialHelper.UnsafeGetPassword(credential), partsWithSlashDelimiter[0]); } } else if (partsWithSlashDelimiter.Length == 1 && partsWithAtDelimiter.Length == 2) { if (!string.IsNullOrEmpty(partsWithAtDelimiter[0]) && !string.IsNullOrEmpty(partsWithAtDelimiter[1])) { credential = new NetworkCredential(partsWithAtDelimiter[0], NetworkCredentialHelper.UnsafeGetPassword(credential), partsWithAtDelimiter[1]); } } } }
internal static NetworkCredential GetNetworkCredentialOrDefault(NetworkCredential credential) { // Because CredentialCache.DefaultNetworkCredentials is not immutable, we dont use it in our OM. Instead we // use an empty NetworkCredential to denote the default credentials. if (NetworkCredentialHelper.IsNullOrEmpty(credential)) { return(CredentialCache.DefaultNetworkCredentials); } return(credential); }
internal static NetworkCredential GetNetworkCredentialsCopy(NetworkCredential networkCredential) { NetworkCredential result; if (networkCredential != null && !NetworkCredentialHelper.IsDefault(networkCredential)) { result = new NetworkCredential(networkCredential.UserName, networkCredential.Password, networkCredential.Domain); } else { result = networkCredential; } return(result); }
internal static NetworkCredential GetNetworkCredentialsCopy(NetworkCredential networkCredential) { NetworkCredential result; if (networkCredential != null && !NetworkCredentialHelper.IsDefault(networkCredential)) { result = new NetworkCredential(NetworkCredentialHelper.UnsafeGetUsername(networkCredential), NetworkCredentialHelper.UnsafeGetPassword(networkCredential), NetworkCredentialHelper.UnsafeGetDomain(networkCredential)); } else { result = networkCredential; } return(result); }
internal static NetworkCredential GetNetworkCredentialOrDefault(NetworkCredential credential) { // because of VSW 564452, we dont use CredentialCache.DefaultNetworkCredentials in our OM. Instead we // use an empty NetworkCredential to denote the default credentials if (NetworkCredentialHelper.IsNullOrEmpty(credential)) { // FYI: this will fail with SecurityException in PT due to Demand for EnvironmentPermission. // Typically a PT app should not have access to DefaultNetworkCredentials. If there is a valid reason, // see UnsafeGetDefaultNetworkCredentials. return(CredentialCache.DefaultNetworkCredentials); } else { return(credential); } }
internal static EndpointIdentity CreateWindowsIdentity(NetworkCredential serverCredential) { if (serverCredential != null && !NetworkCredentialHelper.IsDefault(serverCredential)) { string upn; if (serverCredential.Domain != null && serverCredential.Domain.Length > 0) { upn = serverCredential.UserName + "@" + serverCredential.Domain; } else { upn = serverCredential.UserName; } return(EndpointIdentity.CreateUpnIdentity(upn)); } return(CreateWindowsIdentity()); }
internal static string AppendWindowsAuthenticationInfo(string inputString, NetworkCredential credential, AuthenticationLevel authenticationLevel, TokenImpersonationLevel impersonationLevel) { const string delimiter = "\0"; // nonprintable characters are invalid for SSPI Domain/UserName/Password if (NetworkCredentialHelper.IsDefault(credential)) { string sid = NetworkCredentialHelper.GetCurrentUserIdAsString(credential); return(string.Concat(inputString, delimiter, sid, delimiter, AuthenticationLevelHelper.ToString(authenticationLevel), delimiter, TokenImpersonationLevelHelper.ToString(impersonationLevel))); } return(string.Concat(inputString, delimiter, credential.Domain, delimiter, credential.UserName, delimiter, credential.Password, delimiter, AuthenticationLevelHelper.ToString(authenticationLevel), delimiter, TokenImpersonationLevelHelper.ToString(impersonationLevel))); }