I have a Windows Forms Control that I'm using in a web page and I want to get access to the HttpContext of the page from the control. Is there any way to do that?

Recommended Answers

All 13 Replies

"this"? That's not very descriptive. If it's not the answer or porn I'm going to be upset

"this"? That's not very descriptive. If it's not the answer or porn I'm going to be upset

Check out this related article.

Also, check out this other related article from MSDN.

I checked them, but they just made me wonder if there's a less confusing solution

All I really want is the current user's login name. But I cant use WindowsIdentity.GetCurrent() because there are some cases where calling that from the control will return a different user than calling it from the code behind (which has the value I need). I thought if I could get HttpContext.Current it would have the right value, but that plan kind of relied on it being easy to do

Not sure which machines user you are trying to obtain, but have you tried:

System.Security.Principal.WindowsIdentity.GetCurrent();

yeah
It only works 99% of the time. There's a chance it returns the wrong user because of how the web app works. I'd explain but that would be boring. The point is the code behind for the page would know the right user 100% of the time so I thought getting at the HttpContext might be a solution. Maybe there's some other way to see what the code behind sees

Is this a web server app and your trying to obtain the username of the logged in user currently accessing the form?

It's a windows forms control embeded in an aspx page. Kind of like an activex control. It's separate from the web app. It seems to run under a different process. And I'm trying to obtain the username of the logged in user currently accessing the web app or page or something

How about GetCredential?

Uri uri = new Uri("http://mysite.com/");
            ICredentials credentials = CredentialCache.DefaultCredentials;
            NetworkCredential credential = credentials.GetCredential(uri, "Basic");

            string user = credential.UserName;

For some reason that's just an empty string

Is your website using IIS Setting--Anonymous Access/Authentication?
If so, try disabling and turn on Integrated Windows Authentication, then retry your call to: System.Web.HttpContext.Current.User.Identity.Name

It's already set to Integrated Windows Authentication

I think I might be asking for something that's impossible

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.