Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

iis - How to decrypt the cookie inside custom URL rewrite provider?

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 enter image description here

Rewrite Settings in IIS - InBound

Redirect URL

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

Conditions

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...