如何解密SAML令牌

时间:2011-03-28 19:18:37

标签: saml encryption adfs

有这样的桌面应用程序  它似乎工作但最终作为令牌返回加密saml 你能告诉我如何解密吗

class Program
    {
    static void Main(string[] args)
    {
        ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidationCallback;

        var samlToken = GetSamlToken("@domain", "@login", "@password");

        Console.WriteLine(Uri.UnescapeDataString(samlToken));
        Console.ReadLine();
    }
    private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return sslPolicyErrors == SslPolicyErrors.None
            || string.Equals(certificate.Issuer, "CN=Name", StringComparison.InvariantCultureIgnoreCase);
    }
    private static string GetSamlToken(string domain, string userName, string password)
    {

        var acsUrl = "@RPURL";

        var stsUrl = "@stsurl";

        WSTrustChannelFactory trustChannelFactory =
            new WSTrustChannelFactory(new WindowsWSTrustBinding(SecurityMode.TransportWithMessageCredential),
                new EndpointAddress(new Uri(stsUrl)));

        trustChannelFactory.TrustVersion = TrustVersion.WSTrust13;
        trustChannelFactory.Credentials.Windows.ClientCredential.Domain = domain;
        trustChannelFactory.Credentials.Windows.ClientCredential.UserName = userName;
        trustChannelFactory.Credentials.Windows.ClientCredential.Password = password;

        try
        {
            RequestSecurityToken rst =
                new RequestSecurityToken(WSTrust13Constants.RequestTypes.Issue, WSTrust13Constants.KeyTypes.Bearer);
            rst.AppliesTo = new EndpointAddress(acsUrl);
            rst.TokenType = Microsoft.IdentityModel.Tokens.SecurityTokenTypes.Saml2TokenProfile11;

            WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel();
            GenericXmlSecurityToken token = channel.Issue(rst) as GenericXmlSecurityToken;
            string tokenString = token.TokenXml.OuterXml;

            return tokenString;
        }
        finally
        {
            trustChannelFactory.Close();
        }
    }
}

感谢

1 个答案:

答案 0 :(得分:-1)