C#字节[]以友好的网址字符串 [英] C# Byte[] to Url Friendly String

查看:167
本文介绍了C#字节[]以友好的网址字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个简单的网站,我组建了一个快速的验证码发生器,以及我希望通过在页面的URL加密密钥。我大概可以做到这一点作为查询字符串参数很容易,但我希望不是太(只是因为没有别的逃跑查询字符串)...

I'm working on a quick captcha generator for a simple site I'm putting together, and I'm hoping to pass an encrypted key in the url of the page. I could probably do this as a query string parameter easy enough, but I'm hoping not too (just because nothing else runs off the query string)...

我的加密code产生一个​​字节[],然后用Convert.ToBase64String(字节[])成字符串变换。这个字符串,但是,仍然是不太友好的URL,因为它可以包含之类的东西'/'和'='。有谁知道在.NET框架的一个更好的功能为一个字节数组转换为一个url友好的字符串?

My encryption code produces a byte[], which is then transformed using Convert.ToBase64String(byte[]) into a string. This string, however, is still not quite url friendly, as it can contain things like '/' and '='. Does anyone know of a better function in the .NET framework to convert a byte array to a url friendly string?

我知道所有关于System.Web.HttpUtility.UrlEn code()和现金等价物,但是,他们只与查询字符串参数正常工作。如果我url可连接code一个'='的路径里面,我的Web服务器带回一个400错误的请求错误。

I know all about System.Web.HttpUtility.UrlEncode() and its equivalents, however, they only work properly with query string parameters. If I url encode an '=' inside of the path, my web server brings back a 400 Bad Request error.

不管怎么说,而不是一个严重的问题,但希望有人可以给我一个很好的解决方案。

Anyways, not a critical issue, but hoping someone can give me a nice solution

**编辑:只是要绝对确保正是我用字符串做,我想我会提供一个小的详细信息。

** Just to be absolutely sure exactly what I'm doing with the string, I figured I would supply a little more information.

字节[],从我的加密算法结果应通过某种算法来喂食,使之变成一个url友好的字符串。在此之后,它成为一个的XElement,然后将其用作用于XSLT转换源文档的内容,并且被用作锚href属性的一部分。我不相信XSLT转换是造成问题,因为什么是未来通过的道路上似乎是一间codeD查询字符串参数,而导致HTTP 400

The byte[] that results from my encryption algorithm should be fed through some sort of algorithm to make it into a url friendly string. After this, it becomes the content of an XElement, which is then used as the source document for an XSLT transformation, and is used as a part of the href attribute for an anchor. I don't believe the xslt transformation is causing the issues, since what is coming through on the path appears to be an encoded query string parameter, but causes the HTTP 400

我也试过HttpUtility.UrlPathEn code()上的一个base64字符串,但似乎没有这样的伎俩是(我还是最终在我的网址/S)**

I've also tried HttpUtility.UrlPathEncode() on a base64 string, but that doesn't seem to do the trick either (I still end up with '/'s in my url)**

推荐答案

您正在寻找 HttpServerUtility.UrlTokenEn code HttpServerUtility .UrlTokenDe code 的System.Web

他们带code用base64,与更换具有潜在危险的'+'和'/'字符' - '和'_',而不是

They encode in base64, replacing the potentially dangerous '+' and '/' chars with '-' and '_' instead.

MSDN文档

这篇关于C#字节[]以友好的网址字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆