1

Am writing a custom rewrite provider for IIS to send users to different sites depending on whether certain criteria are met. So if the user meets the criteria, the url does not get rewritten and the user can proceed as normal. If the user does not meet the criteria the url gets rewritten to load a different site, whilst appearing to be on the same url.

If set the rewrite section of the provider up like so the url is unaffected and the user is sent to the site as expected.

public string Rewrite(string value)
{
    return value;
}

If set the rewrite section of the provider up like so the url is rewritten and the user is sent the alternate site.

public string Rewrite(string value)
{
    return alternateSite;
}

If however I use the following set up if the user does not match the criteria they end up in the alternate site as expected. However if they do meet the criteria they end up in a redirect loop.

public string Rewrite(string value)
{
    string newVal = alternateSite;
    if (user != null)
    {
        if (user.status == 1)
        {
            newVal = value;
        }
    }
    return newVal;
}

Any ideas how I can prevent this loop and have the site load correctly.

Thanks

EDIT Still no joy with this. I suspect if I could make the provider not perform the rewrite if the user meets the credentials (equivalent to setting the action to none on a standard rewrite rule) then it should continue to load normally. However I have no idea how to do this.

HuwD
  • 1,800
  • 4
  • 27
  • 58

1 Answers1

0

The problem with this was that the provider was only setting the variables for the provider when it was initialized, either that or they were being cached. These values were then being used for multiple sessions on multiple calls. This is fine for a constant like a connection string but not for something like a user id stored in a cookie. Instead I sent the user id through as part of the rewrite itself and split out it out in the provider logic as part of the Rewrite like so:

In IIS {myProvider:{URL}+{HTTP_COOKIE}}

In provider

public string Rewrite(string value)
{
    string[] values = value.Split('+');
    string requestUrl = values[0];
    string cookieStr = "";
    if (values.Length > 1)
    {
        cookieStr = values[1];
    }
    ...

This forces the provider to get fresh data each time and now the redirects go were they are supposed to.

Hope this is useful to someone

HuwD
  • 1,800
  • 4
  • 27
  • 58
  • Hi HowD, Thanks for post, please help me how to decrypt the cookie inside Rewrite method? I am getting Invalid value for 'encryptedTicket' parameter error. Can I create a new post in stack overflow? – kudlatiger Jan 07 '16 at 10:38
  • Here is link to my issue -Here is link -http://stackoverflow.com/questions/34653227/invalid-value-for-encryptedticket-parameter-how-to-decrypt-the-cookie-inside – kudlatiger Jan 07 '16 at 10:52
  • 1
    Have posted an answer on your questions. Hope it helps. – HuwD Jan 07 '16 at 13:52
  • okay I will check and keep you posted. original issue is here, if possible please have a look - http://stackoverflow.com/questions/34506551/reading-cookie-value-using-url-rewrite-provider-module-unable-to-validate-at – kudlatiger Jan 07 '16 at 15:49