/// <summary> /// Helper Method /// </summary> /// <param name="stsUrl"></param> /// <param name="realm"></param> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> private static string GetResponse(string stsUrl, string realm, string username, string password) { RequestSecurityToken rst = new RequestSecurityToken(); rst.RequestType = WSTrustFeb2005Constants.RequestTypes.Issue; // // Bearer token, no encryption // rst.AppliesTo = new EndpointAddress(realm); rst.KeyType = WSTrustFeb2005Constants.KeyTypes.Bearer; WSTrustFeb2005RequestSerializer trustSerializer = new WSTrustFeb2005RequestSerializer(); WSHttpBinding binding = new WSHttpBinding(); binding.Security.Mode = SecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.EstablishSecurityContext = false; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; EndpointAddress address = new EndpointAddress(stsUrl); WSTrustFeb2005ContractClient trustClient = new WSTrustFeb2005ContractClient(binding, address); trustClient.ClientCredentials.UserName.UserName = username; trustClient.ClientCredentials.UserName.Password = password; Message response = trustClient.EndIssue(trustClient.BeginIssue(Message.CreateMessage(MessageVersion.Default, WSTrustFeb2005Constants.Actions.Issue, new RequestBodyWriter(trustSerializer, rst)), null, null)); trustClient.Close(); XmlDictionaryReader reader = response.GetReaderAtBodyContents(); return(reader.ReadOuterXml()); }
private string getResponse(string stsUrl, string realm) { RequestSecurityToken rst = new RequestSecurityToken { RequestType = WSTrustFeb2005Constants.RequestTypes.Issue, AppliesTo = new EndpointAddress(realm), KeyType = WSTrustFeb2005Constants.KeyTypes.Bearer, TokenType = Microsoft.IdentityModel.Tokens.SecurityTokenTypes.Saml11TokenProfile11 }; WSTrustFeb2005RequestSerializer trustSerializer = new WSTrustFeb2005RequestSerializer(); WSHttpBinding binding = new WSHttpBinding(); binding.Security.Mode = SecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.EstablishSecurityContext = false; binding.Security.Message.NegotiateServiceCredential = false; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; EndpointAddress address = new EndpointAddress(stsUrl); using (WSTrustFeb2005ContractClient trustClient = new WSTrustFeb2005ContractClient(binding, address)) { trustClient.ClientCredentials.UserName.UserName = _username; trustClient.ClientCredentials.UserName.Password = _password; Message response = trustClient.EndIssue( trustClient.BeginIssue( Message.CreateMessage( MessageVersion.Default, WSTrustFeb2005Constants.Actions.Issue, new RequestBodyWriter(trustSerializer, rst) ), null, null)); trustClient.Close(); using (XmlDictionaryReader reader = response.GetReaderAtBodyContents()) { return(reader.ReadOuterXml()); } } }
public Message BuildRequestSecurityTokenMessage(Guid contextGuid) { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] keyEntropy = new byte[256 / 8]; rng.GetBytes(keyEntropy); BinarySecretSecurityToken token = new BinarySecretSecurityToken( string.Format( CultureInfo.InvariantCulture, "uuid-{0}-{1}", Guid.NewGuid().ToString(), Interlocked.Increment( ref lockInt).ToString(CultureInfo.InvariantCulture)), keyEntropy); RequestSecurityToken rst = new RequestSecurityToken(Microsoft.IdentityModel.SecurityTokenService.RequestTypes.Issue); rst.TokenType = "http://schemas.xmlsoap.org/ws/2005/02/sc/sct"; rst.RequestType = "http://schemas.xmlsoap.org/ws/2005/02/trust/Issue"; rst.KeySizeInBits = 256; rst.Context = contextGuid.ToString(); rst.Entropy = new Entropy(token.GetKeyBytes()); WSTrustFeb2005RequestSerializer test = new WSTrustFeb2005RequestSerializer(); WSTrustSerializationContext sc = new WSTrustSerializationContext(); WSTrustRequestBodyWriter bw = new WSTrustRequestBodyWriter(rst, test, sc); MessageVersion mv = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10); Message request = Message.CreateMessage(mv, Constants.WsTrust.RequestSecurityTokenIssueAction, bw); request.Headers.ReplyTo = new EndpointAddress("http://www.w3.org/2005/08/addressing/anonymous"); request.Headers.To = new Uri("http://localhost:5725/ResourceManagementService/Alternate"); return(request); }
string getResponse(string stsUrl, string realm) { RequestSecurityToken rst = new RequestSecurityToken { RequestType = WSTrustFeb2005Constants.RequestTypes.Issue, AppliesTo = new EndpointAddress(realm), KeyType = WSTrustFeb2005Constants.KeyTypes.Bearer, TokenType = Microsoft.IdentityModel.Tokens.SecurityTokenTypes.Saml11TokenProfile11 }; WSTrustFeb2005RequestSerializer trustSerializer = new WSTrustFeb2005RequestSerializer(); WSHttpBinding binding = new WSHttpBinding(); binding.Security.Mode = SecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.EstablishSecurityContext = false; binding.Security.Message.NegotiateServiceCredential = false; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; EndpointAddress address = new EndpointAddress(stsUrl); using (WSTrustFeb2005ContractClient trustClient = new WSTrustFeb2005ContractClient(binding, address)) { trustClient.ClientCredentials.UserName.UserName = _username; trustClient.ClientCredentials.UserName.Password = _password; Message response = trustClient.EndIssue( trustClient.BeginIssue( Message.CreateMessage( MessageVersion.Default, WSTrustFeb2005Constants.Actions.Issue, new RequestBodyWriter(trustSerializer, rst) ), null, null)); trustClient.Close(); using (XmlDictionaryReader reader = response.GetReaderAtBodyContents()) return reader.ReadOuterXml(); } }