• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# XmlDsigBase64Transform类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Security.Cryptography.Xml.XmlDsigBase64Transform的典型用法代码示例。如果您正苦于以下问题:C# XmlDsigBase64Transform类的具体用法?C# XmlDsigBase64Transform怎么用?C# XmlDsigBase64Transform使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



XmlDsigBase64Transform类属于System.Security.Cryptography.Xml命名空间,在下文中一共展示了XmlDsigBase64Transform类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        // Encrypt an XML message
        EncryptXML(LoadXMLDoc());

        // Using XmlDsigBase64Transform resolving a Uri.
        Uri baseUri = new Uri("http://www.microsoft.com");
        string relativeUri = "msdn";
        Uri absoluteUri = ResolveUris(baseUri, relativeUri);

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Encrypt the text in the specified XmlDocument.
    private static void EncryptXML(XmlDocument xmlDoc)
    {
        XmlDsigBase64Transform xmlTransform = new XmlDsigBase64Transform();

        // Ensure the transform is using the proper algorithm.
        xmlTransform.Algorithm = SignedXml.XmlDsigBase64TransformUrl;

        // Retrieve the XML representation of the current transform.
        XmlElement xmlInTransform = xmlTransform.GetXml();

        Console.WriteLine("Xml representation of the current transform: ");
        Console.WriteLine(xmlInTransform.OuterXml);

        // Retrieve the valid input types for the current transform.
        Type[] validInTypes = xmlTransform.InputTypes;

        // Verify the xmlTransform can accept the XMLDocument as an
        // input type.
        for (int i=0; i<validInTypes.Length; i++)
        {
            if (validInTypes[i] == xmlDoc.GetType())
            {
                // Demonstrate loading the entire Xml Document.
                xmlTransform.LoadInput(xmlDoc);

                // This transform is created for demonstration purposes.
                XmlDsigBase64Transform secondTransform =
                    new XmlDsigBase64Transform();

                string classDescription = secondTransform.ToString();

                // This call does not perform as expected.
                // LoadInnerXml is overridden by the XmlDsigBase64Transform
                // class, but is stubbed out.
                secondTransform.LoadInnerXml(xmlDoc.SelectNodes("//."));

                break;
            }
        }

        Type[] validOutTypes = xmlTransform.OutputTypes;

        for (int i=0; i<validOutTypes.Length; i++)
        {
            if (validOutTypes[i] == typeof(System.IO.Stream))
            {
                try 
                {
                    Type streamType = typeof(System.IO.Stream);
                    CryptoStream outputStream = (CryptoStream) 
                        xmlTransform.GetOutput(streamType);

                    // Read the CryptoStream into a stream reader.
                    StreamReader streamReader =
                        new StreamReader(outputStream);

                    // Read the stream into a string.
                    string outputMessage = streamReader.ReadToEnd();

                    // Close the streams.
                    outputStream.Close();
                    streamReader.Close();

                    // Display to the console the Xml before and after
                    // encryption.
                    Console.WriteLine("Encoding the following message: " +
                        xmlDoc.InnerText);
                    Console.WriteLine("Message encoded: " + outputMessage);
            }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected exception caught: " +
                        ex.ToString());
                }

                break;
            }
            else
            {
                object outputObject = xmlTransform.GetOutput();
            }
        }
    }

    // Create an XML document with Element and Text nodes.
    private static XmlDocument LoadXMLDoc()
    {
        XmlDocument xmlDoc = new XmlDocument();

        XmlNode mainNode = xmlDoc.CreateNode(
            XmlNodeType.Element,
            "ContosoMessages",
            "http://www.contoso.com");

        XmlNode textNode = xmlDoc.CreateTextNode("Some text to encode.");
        mainNode.AppendChild(textNode);
        xmlDoc.AppendChild(mainNode);

        Console.WriteLine("Created the following XML Document for " +
            "transformation: ");
        Console.WriteLine(xmlDoc.InnerXml);
        return xmlDoc;
    }

    // Resolve the specified base and relative Uri's .
    private static Uri ResolveUris(Uri baseUri, string relativeUri)
    {
        XmlUrlResolver xmlResolver = new XmlUrlResolver();
        xmlResolver.Credentials = 
            System.Net.CredentialCache.DefaultCredentials;

        XmlDsigBase64Transform xmlTransform = new XmlDsigBase64Transform();
        xmlTransform.Resolver = xmlResolver;

        Uri absoluteUri = xmlResolver.ResolveUri(baseUri, relativeUri);

        if (absoluteUri != null)
        {
            Console.WriteLine(
                "Resolved the base Uri and relative Uri to the following:");
            Console.WriteLine(absoluteUri.ToString());
        }
        else
        {
            Console.WriteLine(
                "Unable to resolve the base Uri and relative Uri");
        }
        return absoluteUri;
    }
}
//
开发者ID:.NET开发者,项目名称:System.Security.Cryptography.Xml,代码行数:157,代码来源:XmlDsigBase64Transform

输出:

Created the following XML Document for transformation:
Some text to encode.
Xml representation of the current transform:

Encoding the following message: Some text to encode.
Message encoded: Jmr^
Resolved the base Uri and relative Uri to the following:
http://www.microsoft.com/msdn
This sample completed successfully; press Enter to exit.



注:本文中的System.Security.Cryptography.Xml.XmlDsigBase64Transform类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# XmlDsigC14NTransform类代码示例发布时间:2022-05-24
下一篇:
C# SignedXml类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap