1

I've got a webpart that presents a user on SharePoint with a simple button. On clicking the button I log the user that clicked it, the time and their IP address. The bit I can't figure out is how to find their IP address? Can I get to it through the SharePoint object model or do I have to do something more complicated?

private bool SignInCurrentUser()
        {
            SPWeb web = SPContext.Current.Web;
            SPUser user = web.CurrentUser;
            String address = "?";

            SPList regList = web.Lists["SEED MEng Lab Registration List"];

            SPListItem registration = regList.Items.Add();
            registration["Student"] = user;
            registration["Occurrence"] = DateTime.Now;
            registration["IP Address"] = address;
            registration.Update();

            return true;
        }
Daniel Revell
  • 8,338
  • 14
  • 57
  • 95
  • Similar: http://stackoverflow.com/questions/735350/how-to-get-user-client-ip-address-in-asp-net – Alex Angas Oct 01 '09 at 16:13
  • I made a mistake in my code which I've edited out. SharePoint practice has you disposing of SPWeb's and the like whenever you use them. In this case don't. Your not creating the SPWeb here, only taking a reference to it. Trying to dispose of SPWeb in a function like this will only mess up your session. – Daniel Revell Oct 01 '09 at 16:34

2 Answers2

2

I don't think this is exposed by the SharePoint API, however standard ASP.NET techniques (see one and two) should do it.

Try HttpRequest.UserHostAddress.

Alex Angas
  • 59,219
  • 41
  • 137
  • 210
0

Try System.Web.UI.Page.Request.UserHostAddress.

Amy Struck
  • 61
  • 1
  • 2