public string GetToken(string idpEndpoint, string rstsRealm) { var binding = new WindowsWSTrustBinding(SecurityMode.TransportWithMessageCredential); var factory = new System.ServiceModel.Security.WSTrustChannelFactory(binding, new EndpointAddress(new Uri(idpEndpoint))); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.SupportInteractive = false; var rst = new System.IdentityModel.Protocols.WSTrust.RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new System.IdentityModel.Protocols.WSTrust.EndpointReference(rstsRealm), KeyType = KeyTypes.Bearer, TokenType = "urn:oasis:names:tc:SAML:1.0:assertion" // "urn:oasis:names:tc:SAML:2.0:assertion" }; var channel = factory.CreateChannel(); RequestSecurityTokenResponse response = null; try { var securityToken = channel.Issue(rst, out response); return Serialize(response); }catch { var x = response; } return null; }
//https://leastprivilege.com/2010/10/28/wif-adfs-2-and-wcfpart-6-chaining-multiple-token-services/ //https://msdn.microsoft.com/en-us/library/ee517297.aspx public SecurityToken GetToken(string idpEndpoint, string rstsRealm, string userName, string password) { var binding = new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential); var factory = new System.ServiceModel.Security.WSTrustChannelFactory(binding, new EndpointAddress(new Uri(idpEndpoint))); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.SupportInteractive = false; factory.Credentials.UserName.UserName = userName; factory.Credentials.UserName.Password = password; var rst = new System.IdentityModel.Protocols.WSTrust.RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new System.IdentityModel.Protocols.WSTrust.EndpointReference(rstsRealm), KeyType = KeyTypes.Bearer, TokenType = "urn:oasis:names:tc:SAML:2.0:assertion" }; var channel = factory.CreateChannel(); var securityToken = channel.Issue(rst); return securityToken; }
public string GetToken(string idpEndpoint, string rstsRealm) { var binding = new WindowsWSTrustBinding(SecurityMode.TransportWithMessageCredential); var factory = new System.ServiceModel.Security.WSTrustChannelFactory(binding, new EndpointAddress(new Uri(idpEndpoint))); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.SupportInteractive = false; var rst = new System.IdentityModel.Protocols.WSTrust.RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new System.IdentityModel.Protocols.WSTrust.EndpointReference(rstsRealm), KeyType = KeyTypes.Bearer, TokenType = "urn:oasis:names:tc:SAML:1.0:assertion" // "urn:oasis:names:tc:SAML:2.0:assertion" }; var channel = factory.CreateChannel(); RequestSecurityTokenResponse response = null; try { var securityToken = channel.Issue(rst, out response); return(Serialize(response)); }catch { var x = response; } return(null); }
//https://leastprivilege.com/2010/10/28/wif-adfs-2-and-wcfpart-6-chaining-multiple-token-services/ //https://msdn.microsoft.com/en-us/library/ee517297.aspx public SecurityToken GetToken(string idpEndpoint, string rstsRealm, string userName, string password) { var binding = new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential); var factory = new System.ServiceModel.Security.WSTrustChannelFactory(binding, new EndpointAddress(new Uri(idpEndpoint))); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.SupportInteractive = false; factory.Credentials.UserName.UserName = userName; factory.Credentials.UserName.Password = password; var rst = new System.IdentityModel.Protocols.WSTrust.RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new System.IdentityModel.Protocols.WSTrust.EndpointReference(rstsRealm), KeyType = KeyTypes.Bearer, TokenType = "urn:oasis:names:tc:SAML:2.0:assertion" }; var channel = factory.CreateChannel(); var securityToken = channel.Issue(rst); return(securityToken); }
public ActionResult Index() { System.ServiceModel.Security.WSTrustChannelFactory factory = null; System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback); try { // use a UserName Trust Binding for username authentication factory = new System.ServiceModel.Security.WSTrustChannelFactory( new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential), new EndpointAddress("https://win-dcftr8akk4s.dealertracktest.com/adfs/services/trust/13/usernamemixed")); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.UserName.UserName = "******"; factory.Credentials.UserName.Password = "******"; factory.Credentials.SupportInteractive = false; factory.Credentials.UseIdentityConfiguration = true; var rst = new System.IdentityModel.Protocols.WSTrust.RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = new EndpointReference("https://win-dcftr8akk4s.dealertracktest.com/publish/"),// KeyType = KeyTypes.Bearer, // TokenType= "urn:oasis:names:tc:SAML:2.0:assertion", // ReplyTo ="https://sts.pcmidev.com/adfs/services/trust/13/issuedtokenmixedsymmetricbasic256" //RequestDisplayToken = true, }; System.ServiceModel.Security.IWSTrustChannelContract channel = factory.CreateChannel(); System.IdentityModel.Protocols.WSTrust.RequestSecurityTokenResponse rstr; var token = channel.Issue(rst, out rstr) as GenericXmlSecurityToken; string Site = "https://win-dcftr8akk4s.dealertracktest.com/publish/"; var prepareToken = WrapInSoapMessage(token, Site); var samlServer = Site.EndsWith("/") ? Site : Site + "/"; var stringData = $"wa=wsignin1.0&wresult={HttpUtility.UrlEncode(prepareToken)}&wctx={HttpUtility.UrlEncode("rm=1&id=passive&ru=/home")}"; var cookies = new CookieContainer(); var request = WebRequest.Create(samlServer) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = cookies; request.AllowAutoRedirect = true; var data = Encoding.UTF8.GetBytes(stringData); request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (var response = request.GetResponse() as HttpWebResponse) { using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { var responseFromServer = reader.ReadToEnd(); } } } } finally { if (factory != null) { try { factory.Close(); } catch (CommunicationObjectFaultedException) { factory.Abort(); } } } Response.Redirect("https://win-dcftr8akk4s.dealertracktest.com/publish/"); //HttpContext.Current.RewritePath("https://win-dcftr8akk4s.dealertracktest.com/publish/"); return(View("home")); }