且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解密自定义URL重写提供程序内的cookie?

更新时间:2022-10-24 08:04:47

如果有任何其他Cookie的域,他们将作为val [1]的一部分包含在一个长字符串。我有相当的困难使IIS可靠地通过只有一个cookie,所以我把所有的cookie拉到val [1]字符串,然后拆分成一个所有的cookie值的数组,然后只是选择我需要的一个。如果有疑问,您的提供程序会把val [1]字符串作为客户错误,以便您可以看到它看到了什么。

  throw new Exception(val [1]); 

一旦你可以看到实际收到的内容,你就会知道如何分割。


I have a website where I created a custom cookie and I am trying to read the cookie value inside my Custom Rewrite Provider running in IIS

Question in short: How to decrypt the cookie inside custom URL rewrite provider?

Below is the code for creating custom cookie

     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
           1,                                    
           model.Email,                          
           DateTime.Now,                          
           DateTime.Now.AddDays(7),          
           true,                         
           "deepak",                             
           FormsAuthentication.FormsCookiePath);  

        string encryptedTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie fCookie = new HttpCookie("customCookie", encryptedTicket);
        fCookie.Expires = DateTime.Now.AddDays(7);
        fCookie.Path = "/";
        Response.Cookies.Add(fCookie);

Below code is to read the cookie value inside my Custom Rewrite Provider running in IIS

   public class ParseUserNameProvider : IRewriteProvider, IProviderDescriptor
   {
    public IEnumerable<SettingDescriptor> GetSettings()
    {
        throw new NotImplementedException();
    }

    public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
    {}

    public string Rewrite(string value)
    {
        string[] val = value.Split('=');
        string name = "";
        if (val != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(val[1]);
            if(authTicket!=null)
            {
                name = authTicket.Name;
            }
        }
        return name;
    }
}

Error raised as shown below

Rewrite Settings in IIS - InBound

Redirect URL

http://1x2.xx.1x8.x8:1111/Report/Report?name={ParseUserNameProvider:{C:0}}

Conditions

I learned this from : http://www.iis.net/learn/extensions/url-rewrite-module/developing-a-custom-rewrite-provider-for-url-rewrite-module

Note: This post is NOT duplicate of Custom Rewrite Provider for URL Rewrite Module because I am getting different error.

If there are any other Cookies with that domain they'll be included as part of val[1] in one long string. I had considerable difficulty making IIS reliably pass through just one cookie so I pulled though all the cookies into val[1] string and then split that into an array of all the cookie values then just selected the one I needed. If in doubt get your provider to out put the val[1] string as a customer error so you can see what it's seeing.

        throw new Exception(val[1]);

Once you can see what's actually being received you work out how you need to split it.