如何使用MachineKey.Protect的饼干吗? [英] How to use MachineKey.Protect for a cookie?

查看:256
本文介绍了如何使用MachineKey.Protect的饼干吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要加密,我用一个cookie的ID。我使用ASP.NET 4.5,所以我想用 MachineKey.Protect 来做到这一点。



代码



 公共静态字符串保护(字符串文本,字符串目的)
{
如果(string.IsNullOrEmpty(文本))
返回的String.Empty;

字节[] =流Encoding.Unicode.GetBytes(文本);
字节[] = encodedValue MachineKey.Protect(流的目的);
返回HttpServerUtility.UrlTokenEncode(encodedValue);
}

公共静态字符串撤消(字符串文本字符串用)
{
如果(string.IsNullOrEmpty(文本))
返回的String.Empty ;

字节[] =流HttpServerUtility.UrlTokenDecode(文本);
字节[] = decodedValue MachineKey.Unprotect(流的目的);
返回HttpServerUtility.UrlTokenEncode(decodedValue);
}

当我用下面的测试数据:



保护()




输入:775119337 < (饼干)HyV7ShLrb61cm9HWoHl2lUJtGMlMxLn60q27xwl7Ae1wpv31p7sJqfRDD8TMoSR8n8PPN1K7k7LsrjqWH6A-P17OblK3MApsDQRQLa8xj9A1




<$ C:/ p>

$ C>撤消():




输出:NwA3ADUAMQAxADkAMwAzADcA0


< /块引用>

的输出是不正确的,当然,这应该是原始ID I输入



如何我得到使用解密的cookie MachineKey.UnProtect()


解决方案

decodedValue 是你传递给 MachineKey.Protect()。结果
这不是UrlTokenEncoded字节;这是Unicode编码的字节。



您需要调用 Encoding.Unicode.GetString()






从OP:

 公共静态字符串保护(字符串文本字符串用)
{
如果(string.IsNullOrEmpty(文本))
返回NULL;

字节[] =流Encoding.UTF8.GetBytes(文本);
字节[] = encodedValue MachineKey.Protect(流的目的);
返回HttpServerUtility.UrlTokenEncode(encodedValue);
}

公共静态字符串撤消(字符串文本字符串用)
{
如果(string.IsNullOrEmpty(文本))
返回NULL;

字节[] =流HttpServerUtility.UrlTokenDecode(文本);
字节[] = decodedValue MachineKey.Unprotect(流的目的);
返回Encoding.UTF8.GetString(decodedValue);
}


I want to encrypt the ID that I am using in a cookie. I am using ASP.NET 4.5 so I want to use MachineKey.Protect to do it.

Code

    public static string Protect(string text, string purpose)
    {
        if (string.IsNullOrEmpty(text))
            return string.Empty;

        byte[] stream = Encoding.Unicode.GetBytes(text);
        byte[] encodedValue = MachineKey.Protect(stream, purpose);
        return HttpServerUtility.UrlTokenEncode(encodedValue);
    }

    public static string Unprotect(string text, string purpose)
    {
        if (string.IsNullOrEmpty(text))
            return string.Empty;

        byte[] stream = HttpServerUtility.UrlTokenDecode(text);
        byte[] decodedValue = MachineKey.Unprotect(stream, purpose);
        return HttpServerUtility.UrlTokenEncode(decodedValue);
    }

When I use the following test data:

Protect():

Input: 775119337

Output: (Cookie) "HyV7ShLrb61cm9HWoHl2lUJtGMlMxLn60q27xwl7Ae1wpv31p7sJqfRDD8TMoSR8n8PPN1K7k7LsrjqWH6A-P17OblK3MApsDQRQLa8xj9A1"

UnProtect():

Output: "NwA3ADUAMQAxADkAMwAzADcA0"

The output isn't correct, of course, it should be the original ID I Input.

How do I get decrypt the cookie using MachineKey.UnProtect()?

解决方案

decodedValue is the bytes you passed to MachineKey.Protect().
This is not UrlTokenEncoded; it's Unicode-encoded bytes.

You need to call Encoding.Unicode.GetString().


From the OP:

public static string Protect(string text, string purpose)
{
    if (string.IsNullOrEmpty(text))
        return null;

    byte[] stream = Encoding.UTF8.GetBytes(text);
    byte[] encodedValue = MachineKey.Protect(stream, purpose);
    return HttpServerUtility.UrlTokenEncode(encodedValue);
}

public static string Unprotect(string text, string purpose)
{
    if (string.IsNullOrEmpty(text))
        return null;

    byte[] stream = HttpServerUtility.UrlTokenDecode(text);
    byte[] decodedValue = MachineKey.Unprotect(stream, purpose);
    return Encoding.UTF8.GetString(decodedValue);
}

这篇关于如何使用MachineKey.Protect的饼干吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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