当前位置: 首页>>代码示例>>C#>>正文


C# Security.SanitizeHTML方法代码示例

本文整理汇总了C#中Security.SanitizeHTML方法的典型用法代码示例。如果您正苦于以下问题:C# Security.SanitizeHTML方法的具体用法?C# Security.SanitizeHTML怎么用?C# Security.SanitizeHTML使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Security的用法示例。


在下文中一共展示了Security.SanitizeHTML方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestSanitizeHTMLTagIsFirst

        public void TestSanitizeHTMLTagIsFirst()
        {
            //arrange
            var secure = new Security();

            //act
            var result = secure.SanitizeHTML("<script>evil</script><b>hello</b>");

            //assert
            Assert.AreEqual(@"&lt;script&gt;evil&lt;/script&gt;<b>hello</b>", result);
        }
开发者ID:tcMichaelson,项目名称:Day01,代码行数:11,代码来源:PreventXSS.cs

示例2: TestSanitizeHTMLNoUnsafe

        public void TestSanitizeHTMLNoUnsafe()
        {
            //arrange
            var secure = new Security();

            //act
            var result = secure.SanitizeHTML("<b>hello</b><body>evil</body>");

            //assert
            Assert.AreEqual(@"<b>hello</b>&lt;body&gt;evil&lt;/body&gt;", result);
        }
开发者ID:tcMichaelson,项目名称:Day01,代码行数:11,代码来源:PreventXSS.cs

示例3: SanitizeHTMLTest

        public void SanitizeHTMLTest()
        {
            // Arrange - Create an instance of any class that you need to test
            var htmlSanitizer = new Security();

            // Act - Use this section to perform the action that you are testing. Typically, you call a method in this section.

            var htmlStr = "<b>hello</b><script>evil</script>"; // Initialize string variable with string content
            var result = htmlSanitizer.SanitizeHTML(htmlStr); // Run test method to ensure proper functioning

            // Assert - Use this section to assert that a certain condition is true.

            Assert.AreNotEqual(result, "<b>hello</b>&lt;script&gt;evil&lt/script&gt;");
            // Assert.AreEqual(result, "<b>hello</b>&lt;script&gt;evil&lt/script&gt;");
        }
开发者ID:nfwaldron,项目名称:CoderCamps-Projects,代码行数:15,代码来源:TestSecurity.cs

示例4: TestTags

        public void TestTags()
        {
            // Arrange
            var security = new Security();

            //allowed tags
            var whitelist = new List<string>();
            whitelist.Add("<b>");
            whitelist.Add("</b>");
            whitelist.Add("<p>");
            whitelist.Add("</p>");
            whitelist.Add("<table>");
            whitelist.Add("</table>");
            whitelist.Add("<ul>");
            whitelist.Add("</ul>");
            whitelist.Add("<ol>");
            whitelist.Add("</ol>");
            whitelist.Add("<li>");
            whitelist.Add("</li>");
            whitelist.Add("<tr>");
            whitelist.Add("</tr>");
            whitelist.Add("<td>");
            whitelist.Add("</td>");
            whitelist.Add("<th>");
            whitelist.Add("</th>");
            whitelist.Add("<h1>");
            whitelist.Add("</h1>");
            whitelist.Add("<h2>");
            whitelist.Add("</h2>");
            whitelist.Add("<h3>");
            whitelist.Add("</h3>");
            whitelist.Add("<h4>");
            whitelist.Add("</h4>");
            whitelist.Add("<h5>");
            whitelist.Add("</h5>");
            whitelist.Add("<h6>");
            whitelist.Add("</h6>");

            string testing = @"<></> <> </> <img/> <script></script><span><div>text test</div></span> <a href=""></a>";
            string correct = @"&lt;&gt;&lt;/&gt; &lt;&gt; &lt;/&gt; &lt;img/&gt; &lt;script&gt;&lt;/script&gt;&lt;span&gt;&lt;div&gt;text test&lt;/div&gt;&lt;/span&gt; &lt;a href=""&gt;&lt;/a&gt;";

            // Act
            var result = security.SanitizeHTML(testing, whitelist);

            // Assert
            Assert.AreNotEqual(testing, result);
            Assert.AreEqual(correct, result);
        }
开发者ID:GBoh,项目名称:CCweek1,代码行数:48,代码来源:SanitizeTests.cs


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