コード例 #1
0
ファイル: FedAuthSupport.cs プロジェクト: wchin4ms/KalAcademy
            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);
            }
コード例 #2
0
        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();
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Issues the token
        /// Mostly copied from Service References
        /// </summary>
        private GenericXmlSecurityToken IssueToken()
        {
            _logger.WriteDebug("Issue Token");
            var issuerEndpoint = FindIssuerEndpoint();

            var requestSecurityToken = new RequestSecurityToken
            {
                RequestType = RequestTypes.Issue,
                AppliesTo   = new EndpointReference(_infoShareWSAppliesTo.Value.AbsoluteUri),
                KeyType     = System.IdentityModel.Protocols.WSTrust.KeyTypes.Symmetric
            };

            using (var factory = new WSTrustChannelFactory((WS2007HttpBinding)issuerEndpoint.Binding, issuerEndpoint.Address))
            {
                ApplyCredentials(factory.Credentials);
                ApplyTimeout(factory.Endpoint, _connectionParameters.IssueTimeout);

                factory.TrustVersion = TrustVersion.WSTrust13;
                factory.Credentials.SupportInteractive = false;

                WSTrustChannel channel = null;
                try
                {
                    _logger.WriteDebug($"Issue Token for AppliesTo[{requestSecurityToken.AppliesTo.Uri}]");
                    channel = (WSTrustChannel)factory.CreateChannel();
                    RequestSecurityTokenResponse requestSecurityTokenResponse;
                    return(channel.Issue(requestSecurityToken, out requestSecurityTokenResponse) as GenericXmlSecurityToken);
                }
                catch
                {
                    // Fallback to 10.0.X and 11.0.X configuration using relying party per url like /InfoShareWS/API25/Application.svc
                    requestSecurityToken.AppliesTo = new EndpointReference(_serviceUriByServiceName[Application25].AbsoluteUri);
                    _logger.WriteDebug($"Issue Token for AppliesTo[{requestSecurityToken.AppliesTo.Uri}] as fallback on 10.0.x/11.0.x");
                    RequestSecurityTokenResponse requestSecurityTokenResponse;
                    return(channel.Issue(requestSecurityToken, out requestSecurityTokenResponse) as GenericXmlSecurityToken);
                }
                finally
                {
                    if (channel != null)
                    {
                        channel.Abort();
                    }
                    factory.Abort();
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Issues the token
        /// Mostly copied from Service References
        /// </summary>
        public void IssueToken()
        {
            var requestSecurityToken = new RequestSecurityToken
            {
                RequestType = RequestTypes.Issue,
                AppliesTo   = new EndpointReference(uris["Application25"].AbsoluteUri),
                KeyType     = System.IdentityModel.Protocols.WSTrust.KeyTypes.Symmetric,
            };

            requestSecurityToken.TokenType = SamlSecurityTokenHandler.Assertion;
            //This should have worked directly but I don't know why it doesn't.
            //using (var factory = new WSTrustChannelFactory(this.issuerServiceEndpoint))
            using (var factory = new WSTrustChannelFactory((WS2007HttpBinding)this.issuerServiceEndpoint.Binding, this.issuerServiceEndpoint.Address))
            {
                ApplyCredentials(factory.Credentials);

                //Apply the connection timeout to the token issue process
                ApplyTimeout(factory.Endpoint, IssueTimeout);

                factory.TrustVersion = TrustVersion.WSTrust13;
                factory.Credentials.SupportInteractive = false;
                WSTrustChannel channel = null;

                try
                {
                    channel = (WSTrustChannel)factory.CreateChannel();
                    RequestSecurityTokenResponse requestSecurityTokenResponse;
                    this.issuedToken = channel.Issue(requestSecurityToken, out requestSecurityTokenResponse) as GenericXmlSecurityToken;
                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (channel != null)
                    {
                        channel.Abort();
                    }

                    factory.Abort();
                }
            }
        }
コード例 #5
0
        private static SecurityToken GetSamlToken(string realm, string stsEndpoint, ClientCredentials clientCredentials)
        {
            using (var factory = new WSTrustChannelFactory(
                       new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
                       new EndpointAddress(new Uri(stsEndpoint))))
            {
                factory.Credentials.UserName.UserName = clientCredentials.UserName.UserName;
                factory.Credentials.UserName.Password = clientCredentials.UserName.Password;
                factory.TrustVersion = TrustVersion.WSTrust13;

                WSTrustChannel channel = null;

                try
                {
                    var rst = new RequestSecurityToken
                    {
                        RequestType = WSTrust13Constants.RequestTypes.Issue,
                        AppliesTo   = new EndpointAddress(realm),
                        KeyType     = KeyTypes.Bearer,
                    };

                    channel = (WSTrustChannel)factory.CreateChannel();

                    RequestSecurityTokenResponse response;
                    var token = channel.Issue(rst, out response);

                    return(token);
                }
                finally
                {
                    if (channel != null)
                    {
                        channel.Abort();
                    }

                    factory.Abort();
                }
            }
        }