private SecurityToken CreateChannelAndFetchToken(WSTrustChannelFactory trustChannelFactory, out RequestSecurityTokenResponse rstr) { trustChannelFactory.TrustVersion = TrustVersion.WSTrust13; trustChannelFactory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; SecurityToken result = null; RequestSecurityToken rst = new RequestSecurityToken { RequestType = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue", KeyType = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer", AppliesTo = new EndpointReference(this.serviceUri.AbsoluteUri) }; WSTrustChannel wSTrustChannel = (WSTrustChannel)trustChannelFactory.CreateChannel(); try { result = wSTrustChannel.Issue(rst, out rstr); if (wSTrustChannel.State == CommunicationState.Opened || wSTrustChannel.State == CommunicationState.Opening) { wSTrustChannel.Close(TimeSpan.FromSeconds(5.0)); } } finally { if (wSTrustChannel.State != CommunicationState.Closed) { wSTrustChannel.Abort(); } } return(result); }
private GenericXmlSecurityToken RequestTrustToken(string userName, string password, Uri appliesToUrl) { WSTrustChannel channel = null; try { channel = CreateWSTrustChannel(userName, password); var request = new RequestSecurityToken(RequestTypes.Issue) { AppliesTo = new EndpointReference(appliesToUrl.OriginalString) }; RequestSecurityTokenResponse response; return(channel.Issue(request, out response) as GenericXmlSecurityToken); } catch { if ((channel != null) && (channel.State == CommunicationState.Faulted)) { channel.Abort(); } throw; } finally { if ((channel != null) && (channel.State != CommunicationState.Faulted)) { channel.Close(); } } }