Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a method in controller to get the ip address of current requesting user for my site.
C#
public class IpController : ApiController
{
    public string GetIp()
    {
        return GetClientIp();
    }

    private string GetClientIp(HttpRequestMessage request = null)
    {
        request = request ?? Request;

        if (request.Properties.ContainsKey("MS_HttpContext"))
        {
            return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
        }

        else if (HttpContext.Current != null)
        {
            return HttpContext.Current.Request.UserHostAddress;
        }
        else
        {
            return null;
        }
    }

}

VB
Now,i want to add this ip address(entry or ip address of current requesting user) in iis. I am working with web api and iis version7. How can i get this ip address and add it to iis programatically in my service class.

Thank you.
Posted
Updated 21-Apr-15 21:44pm
v2
Comments
virusstorm 22-Apr-15 9:56am    
First, I don't recommend doing this from your web application. The user account your application runs under need to be an administrator which opens you up to huge security risks. You should look at some sort of queuing mechanism to send a request to modify the IIS settings.

To accomplish what you are looking for, I suggest looking at the API for IIS:
https://msdn.microsoft.com/en-us/library/aa347649%28VS.90%29.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900