public async Task <string> GetServiceTokenAsync(string username, string password, string serviceTarget, string servicePolicy) { if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException("username"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } if (string.IsNullOrEmpty(serviceTarget)) { throw new ArgumentNullException("serviceTarget"); } await this.InitFederationProviderInfoForUserAsync(username); UserRealmInfo userRealm = await this.GetUserRealmAsync(username); if (userRealm.IsFederated) { string partnerTicketFromAdfs = await this.GetPartnerTicketFromAdfsAsync(userRealm.STSAuthUrl, username, password); return(await this.GetServiceTokenAsync(partnerTicketFromAdfs, serviceTarget, servicePolicy)); } string securityXml = this.BuildWsSecurityUsingUsernamePassword(username, password); return(await this.GetServiceTokenAsync(securityXml, serviceTarget, servicePolicy)); }
private async Task <UserRealmInfo> GetUserRealmAsync(string login) { if (string.IsNullOrWhiteSpace(login)) { throw new ArgumentNullException("login"); } string userRealmServiceUrl = this.UserRealmServiceUrl; string body = string.Format(CultureInfo.InvariantCulture, IdcrlMessageConstants.GetUserRealmMessage, new object[1] { Uri.EscapeDataString(login) }); XDocument xDocument = await this.DoPostAsync(userRealmServiceUrl, IdcrlMessageConstants.GetUserRealmContentType, body, null); XAttribute xAttribute = xDocument.Root.Attribute("Success"); if (xAttribute != null && string.Compare(xAttribute.Value, "true", StringComparison.OrdinalIgnoreCase) == 0) { XElement xElement = xDocument.Root.Element("NameSpaceType"); if (xElement == null) { this._Logger?.LogError("There is no NameSpaceType element in the response when get user realm for user {0}", login); throw IdcrlAuth.CreateIdcrlException(-2147186539); } if (string.Compare(xElement.Value, "Federated", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(xElement.Value, "Managed", StringComparison.OrdinalIgnoreCase) != 0) { this._Logger?.LogError("Unknown namespace type for user {0}", login); throw IdcrlAuth.CreateIdcrlException(-2147186539); } UserRealmInfo userRealmInfo = new UserRealmInfo { IsFederated = (0 == string.Compare(xElement.Value, "Federated", StringComparison.OrdinalIgnoreCase)) }; xElement = xDocument.Root.Element("STSAuthURL"); if (xElement != null) { userRealmInfo.STSAuthUrl = xElement.Value; } if (userRealmInfo.IsFederated && string.IsNullOrEmpty(userRealmInfo.STSAuthUrl)) { this._Logger?.LogError("User {0} is a federated account, but there is no STSAuthUrl for the user.", login); throw CreateIdcrlException(-2147186539); } this._Logger?.LogDebug("User={0}, IsFederated={1}, STSAuthUrl={2}", login, userRealmInfo.IsFederated, userRealmInfo.STSAuthUrl); return(userRealmInfo); } this._Logger?.LogError("Failed to get user's realm for user {0}", login); throw CreateIdcrlException(-2147186539); }