views:

566

answers:

3

Hi all:

I need to redirect a user to a different page if they visit a certain set of pages in a web. There is one condition: I'm not allowed to touch the IIS. I have googled around and found a thing called the custom HttpHandler for WSS 3.0. It sounded like something that I can use to capture the URL the user enters (the most malicious way to get to a page that should really be redirected) and redirect them to another page/web. But having not have the chance to use it yet, I was wondering am I on the right track by using a Custom HttpHandler to redirect a user to a different page in Sharepoint using C#?

Many thanks.

+1  A: 

HttpHandlers are used by IIS to "handle" different document types, for instance you have separate .asmx handle, .aspx handler, .ascx handler, etc.

Look into SPUtility.Redirect

You can use the SPUtility.Redirect method whenever you want to direct the user to a different page. For example, you might create a landing page that determines the user's role membership, and based on that information you can redirect them to an appropriate page. Or, based on the contents of a query string issued by the user's browser, you might redirect them to a page that can process the query string, such as the Search Center results page.

Alternatively, you can look into Using Disposable Windows SharePoint Services Objects.

--EDIT--

This is in response to your comment; you are thinking in the right direction

  1. Create a custom HttpHandler; Example 1, Example 2
  2. Whenever a user requests a url, your custom HttpHandler is going to process that.
  3. Redirect() if you like the url, else otherwise.

But for your Custom HttpHandler to work, it should be called first - so in your config file you will have to provide the value in path. Usually an extension is added here. But you can replace that with a * to work for all request. I believe that would work.

<httpHandlers>
    <add verb="*" path="*.aspx" type="MyNameSpace.MyHttpHandler, MyAssemblyName" />
</httpHandlers>

--EDIT--

This is in response to your comment. Assuming that you have "access" to pages so that you can write javascript in it, you can use the javascript in following way.

<script language=JavaScript>
function RedirectIfUnAuthorized()
{
    //Get the location of the current page.
    var currentUrl = new String( document.location.href ) 

    //See if it belongs to the urls you are looking for; redirect if so.
    if (currentUrl == "http://www.thisUrl.com/page1.aspx") {Response.Redirect("http://www.GOOGLE.com")}
    if (currentUrl == "http://www.thisUrl.com/page2.aspx") {Response.Redirect("http://www.BING.com")}
    if (currentUrl == "http://www.thisUrl.com/page3.aspx") {Response.Redirect("http://www.someother.com")}
}
</script>

You may call the above javascript in the page's OnLoad event.

KMan
@KMan: thanks for the quick response. I had a look into it before the HttpHandler, but I couldnt find a single entry point in my application where I can insert the Redirect, sicne the user can just enter a url into the browser. I may be wrong since I'm new to .Net and Sharepoint. Initially I was thinking along the lines of like a Java servlet filter, where the filter can detect and do something with the url, but I have a feeling I'm complicating things already.
BeraCim
@BeraCim: Please see my edit in response to your comment.
KMan
@Kman: thanks for your response. What if now I tell you my biggest challenge (yet) is that I cannot touch IIS or the web.config? I'm thinking of using javascript in the aspx pages to run a c# class that reads a list of urls that need to be redirected, and redirect the user in the c# class using SPUtility.Redirect. Havent tried it yet, as I do not have a good feeling on how it would turn out. Are there any other alternatives I can try?
BeraCim
@BeraCim: Please see my edit in response to your comment.
KMan
@KMan: I think you have listed almost every single way to solve my problem. Thanks, you're a champion.
BeraCim
A: 

Are you allowed to deploy code on the server? Or is that touching IIS too?

(SharePoint makes changes to web.config too. If you're allowed to deploy code, so could you. If you don't tell your admins they probably wouldnt even notice.)

You can 'deploy' your web.config changes through an SPWebConfigModification and deploy any web.config redirections or httphandlers that way.

ArjanP
A: 

HTTP handlers are the end point objects in ASP.NET pipeline and an HTTP Handler essentially processes the request and produces the response. For example an ASP.NET Page is an HTTP Handler.

HTTP Modules are objects which also participate the pipeline but they work before and after the HTTP Handler does its job, and produce additional services within the pipeline (for example associating session within a request before HTTP handler executes, and saving the session state after HTTP handler has done its job, is basically done by an HTTP module, SessionStateModule)

In your case, HTTPModule will require to redirect the another web page.

using System;
using System.Web;
using System.Web.UI;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace CustomHttpModule
{
    public class HttpModuleImplementation : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            if (context == null)
                throw new ArgumentNullException("Context == null");

            context.AuthorizeRequest += new EventHandler(this.ProcessRequestHandler);
        }
        #endregion

        private void DummpRequest(object sender, EventArgs e)
        {
        }
        //first check that user.identity record exist in database
        //If not then forward user to User registration page
        private void ProcessRequestHandler(object sender, EventArgs e)
        {
            try
            {
                HttpApplication context = (HttpApplication)sender;
                string strAbsoluteUri = context.Request.Url.AbsoluteUri.ToLower();
                                //check if request is accessing aspx page
                if (strAbsoluteUri.Substring(strAbsoluteUri.Length - 5, 5).Contains(".aspx"))
                {
                    string userName = context.User.Identity.Name;
                    //replace Test Module with DB call to validate user data
                    if (!CheckUserInDb(userName))
                    {
                        if (!strAbsoluteUri.Contains("mypage.aspx"))
                            redirectToRegistrationPage(context);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void redirectToRegistrationPage(HttpApplication context)
        {
            context.Response.Redirect("http://" + context.Request.ServerVariables["HTTP_HOST"].ToString() + "Regpage.aspx", false);
        }

        private bool CheckUserInDb(string userName)
        {
                    return true;
        }
    }
}

In SharePoint virtual directory web.config file you have to enter the following entry under section httpModules :

<add name="CustomHttpModule" type="CustomHttpModule.HttpModuleImplementation, CustomHttpModule" />
Zia