client side of WCF using JWT for bearer


 

image

 

1 // 2 // GET: /TodoList/ 3 public async Task<ActionResult> Index() 4 { 5 ServiceReference1.ServiceClient sc = new ServiceReference1.ServiceClient(); 6 7 sc.ClientCredentials.SupportInteractive = false; 8 sc.ClientCredentials.UserName.UserName = "support170"; 9 sc.ClientCredentials.UserName.Password = FRED"; 10 11 // var cssdf = sc.GetData(45); 12 13 // 14 // Retrieve the user's tenantID and access token since they are parameters used 15 // to call the To Do service. 16 // 17 string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value; 18 string accessToken = TokenCacheUtils.GetAccessTokenFromCacheOrRefreshToken(tenantId, todoListResourceId); 19 20 var tokenHandler = new JwtSecurityTokenHandler(); 21 SecurityToken st = tokenHandler.ReadToken(accessToken); 22 23 24 // from http://stackoverflow.com/questions/16312907/delivering-a-jwt-securitytoken-to-a-wcf-client 25 // 26 XmlDocument document = new XmlDocument(); 27 XmlElement element = document.CreateElement("wsse", "BinarySecurityToken", 28 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 29 element.SetAttribute("ValueType", "urn:ietf:params:oauth:token-type:jwt"); 30 element.SetAttribute("EncodingType", 31 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"); 32 UTF8Encoding encoding = new UTF8Encoding(); 33 element.InnerText = Convert.ToBase64String(encoding.GetBytes(accessToken)); 34 35 GenericXmlSecurityToken genericst = new GenericXmlSecurityToken( 36 element, 37 null, 38 st.ValidFrom, 39 st.ValidTo, 40 null, 41 null, 42 null); 43 44 WS2007FederationHttpBinding fedbinding 45 = new WS2007FederationHttpBinding("WS2007FederationHttpBinding_IService"); 46 fedbinding.Security.Mode = WSFederationHttpSecurityMode.TransportWithMessageCredential; 47 fedbinding.Security.Message.IssuedKeyType = System.IdentityModel.Tokens.SecurityKeyType.BearerKey; 48 fedbinding.Security.Message.IssuedTokenType = "urn:ietf:params:oauth:token-type:jwt"; 49 fedbinding.Security.Message.EstablishSecurityContext = false; 50 fedbinding.Security.Message.NegotiateServiceCredential = false; 51 UriBuilder ub = new UriBuilder(sc.Endpoint.Address.Uri); 52 ub.Scheme = Uri.UriSchemeHttps; 53 ub.Port = 44307; 54 ub.Host = "localhost"; 55 56 ServiceReference1.ServiceClient scbearer 57 = new ServiceReference1.ServiceClient(fedbinding, new EndpointAddress(ub.Uri)); 58 59 var svcChannel = scbearer.ChannelFactory.CreateChannelWithIssuedToken(genericst); 60 var cssdf2 = svcChannel.GetData(45); 61 62 } 63

Advertisements

About home_pw@msn.com

Computer Programmer who often does network administration with focus on security servers. Very strong in Microsoft Azure cloud!
This entry was posted in office365. Bookmark the permalink.