Peter's Gekko

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Who is that requesting my web page ?

A quick how to on identifying an user from code. Nothing special, but it works on my (customer's) machine.

The default in ASP.NET is that any anonymous user browsing your site is allowed access to all pages. To change that you modify web.config. How to do that is documented in the comments of the web.config file VS generates. To disable anonymous access change the default

<authorization>
   <allow users="*" /> <!-- Allow all users --
>
</authorization>

to

<authorization>
   <deny users="?"></deny
>
</authorization>

The next step is set up the way the user will be authenticated, this is also set in the web.config. This is the default

<authentication mode="Windows" />

Now asp.net will check the identity of the user to the Windows users known on the machine the web server is running. When you run your apps using localhost your current Windows login will work fine. But when you access the webserver from a remote client you browser will pop up a dialog requesting an userid, pwd combination which is known on the server. The nice thing is that asp.net will keep this info the whole session, browsing from page to page will not pop up a new login dialog.

MyUser = context.User.Identity;

The only problem with this is that you do need the context of the web form. A database component, where there are many reasons to know who is using the data, there is no context. To solve that the HttpContext class has a static (shared) memeber which will return the current context from wherever you are

MyUser = System.Web.HttpContext.Current.User.Identity;

There is an enormous lot more to be said about user identification. None of it is rocket science, just a matter of finding the right classes and members in the framework. I hope this little story has given a first direction.

Peter


Posted Tue, May 11 2004 7:56 AM by pvanooijen

[Advertisement]

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us