Response redirect using IP in dotnetnuke

391 views Asked by At

I am hosting and developer on my DNN portal. I need to redirect users using client IP. I think may be two ideas for this work.

1- DNN Setting

Maybe DNN has settings for it that I can set specific URL for client IP addresses and automatically DNN redirects to specific URL.

I read many topic but I could not find setting to do it.

Is there a way to do this?

2- New Module

I have a ascx that onload method has this code:

var IP = Server.HtmlEncode(Request.UserHostAddress).ToString();         
using (Entities db = new Entities())
{
    var retVal = db.URLAddresses.Where(u => u.IPAdress == IP).FirstOrDefault();
        if (retVal != null)
            Response.Redirect(retVal.URL);
}

But I should add this code to any ascx for redirect using client IP. This is impossible because maybe I haven't source code modules.

I think I should create new module. So I can add it to page. Module changes onload page and redirect to URL using client IP.

In this scenario, I try to create new module but I don't know how I can change onload method each page that is added module to it?

2

There are 2 answers

0
taha mousavi On BEST ANSWER

You can use IHttpModule and make a new Module for Including your class then you should add your IHttpModule to web.config . For e.g

      <add name="YourModule" type="YourAssembly, YourNameSpace" preCondition="managedHandler" />

See this Sites: HTTP Handlers and HTTP Modules Overview and How To Create an ASP.NET HTTP Module

0
Fix It Scotty On

DNN does have a Host setting that will allow or deny access to users logging in based on their IP address. It's in Host Settings > Advanced Settings > Login IP Filters. I don't think that will give you the desired result.

I would not suggest creating a module. It can be difficult copying it to all pages and ensuring one instance is added to every page.

Rather, I would create a skin (theme) token. To do this, create a simple class library project. Create an .ascx and ascx.cs file. You can leave the .ascx empty because you don't have any html to add to the pages. In the .cs, put something like this:

namespace MyCompany.DNN.Skin
{
    public partial class IpRedirect : SkinObjectBase
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Put your redirect logic here
        }
    }
}

Then, in your theme/skin ascx pages, include the following:

<%@ Register TagPrefix="myco" TagName="IPREDIRECT" Src="~/DesktopModules/MyCompany/IpRedirect/IpRedirect.ascx" %>

<myco:IPREDIRECT ID="pageRedirect" runat="server" />

This will ensure that this functionality will execute on all pages in the site that use the skin/theme.