Developer forum

Forum » Development » Intranet - IP
Anders Ebdrup
Reply

Hello! :-)

 

Is it possible to use the Extranet/Intranet module for giving access to restricted pages when the user originates from a specific ip and if not then prompt the user for username and password??

 

Best regards, Anders


Replies

 
Mikkel Ricky
Reply

You could try something like this

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Standard.Page.Loaded)]
public class SignInByIpAddress : Dynamicweb.Extensibility.NotificationSubscriber
{
    public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
    {
        if (Dynamicweb.ExecutingContext.IsFrontEnd())
        {
            var pageView = (args as Dynamicweb.Notifications.Standard.Page.LoadedArgs).pageview;
            if (pageView != null && !pageView.User.LoggedIn)
            {
                var ip = GetIPAddress();
                if (!string.IsNullOrWhiteSpace(ip))
                {
                    var user = Dynamicweb.Modules.UserManagement.User.GetUserByUserName(ip);
                    if (user != null)
                    {

                        var security = new Dynamicweb.Security();
                        security.ExtranetLogin(user.UserName, user.Password);
                    }
                }
            }
        }
    }

    // @see http://stackoverflow.com/a/740431
    protected string GetIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }
}

Best regards,
Mikkel

 
Anders Ebdrup
Reply

Hi Mikkel,

 

Great! Thank you for the response even though I only expected whether this was supported without any custom code, but this piece of code is even better :-)

 

Thanks again, Anders

 
Jeroen Elias
Reply

Hello,

The article at Stackoverflow mentioned above contains a comment from KMX (nov. 15 2012). This guy digged even further and found out that there are actually three possibilities:
HTTP_X_CLUSTER_CLIENT_IP, HTTP_X_FORWARDED_FOR and REMOTE_ADDR

Please read the article.

 

You must be logged in to post in the forum