views:

705

answers:

2

For an ASP.NET application, how can I prompt the user for their username / password when Integrated Windows Authentication is used? I would like to use C# in the code behind of the pages to do this.

I want some pages (e.g. http://intranet/admin/) to prompt for a password, while others automatically go though (via IWA/NTLM). I would then like it to remember that the user has logged in as they visit other pages in the folder / site and offer a logout link for when they are finished.

Edit: What I want to do is send a 401 status and WWW-Authentication headers to the user, so they then log in using their Windows Credentials.

Basically, how Firefox / IE do it if the site isn't trusted.

Edit 2: SharePoint does this kind of thing, where you automatically log in, but can log out and log in as someone else without needing to log out of Windows.

Edit 3:

An example (other than SharePoint): You are logged in as a standard (limited access) Windows / Active Directory user to a trusted site, which passes on your credentials. You then want to log into an admin part of the site with different credentials (but still Windows, not WebForms). The only other way of doing it is if you log off Windows, then log in again. Not practical if you have files open (that you may wish to access) or the administrator can't log in locally (Interactive Login Privilege disabled). Impersonation may be set, as the page allows access to applications the regular user account doesn't (e.g. databases, files, Active Directory admin).

Basically making a page within the site behave as though it is not part of the Trusted Sites zone.

A: 

No seperate code for this.

When the user/client open the particular pate eg., http://intranet/admin/index.aspx

if you configure the particular folder with windows authentication, the browser will

automatically ask for the username and password of the particular user/client.

so this is the configuration that you have to do in the IIS to the particular folder

to which you would like to apply the integrated windows authentication.

solairaja
It doesn't ask for the credentials if it is a trusted site... in some cases I want it to, even if it is a trusted site.
Sam
Its reinventing the wheel - if its is a trusted sites for the particular user. Hope you get the point. The authentication what you need to add is already been take care by the Windows.
solairaja
Like I said, I want to emulate what SharePoint does - where you can logout and log in as a different user without logging out of Windows. That uses Windows Authentication as far as I know.
Sam
Kind of like a 'run as', but for a web page.
Sam
this may be u need -- kindly check my friend.http://dotnetslackers.com/articles/aspnet/Windows-Authentication-using-Form-Authentication.aspx
solairaja
A: 

Sounds like you need to use Impersonation. This allows you to "run as" another Windows user. See here: ASP.NET Impersonation

Here are a couple CodeProject examples:

Windows Impersonation using C#

User Impersonation in .NET

Jim
Would like to do it without passing on the credentials in clear text, but use digest authentication somehow.. is there a way to set asp.net impersonation on the fly, rather than through web.config?
Sam
The link to "User Impersonation in .NET" will take you to a sample project that does it all in code. http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx
Jim
Did you look at this link? Also, did you ever figure this out?
Jim
Had a look, but still not figured out how to do it... SharePoint does it, but since it is not open source, I can't see how it does it.
Sam