views:

1991

answers:

5

Hi!

I want to develop an ASP.NET application that can detect the user logged on a Window Domain. These credentials are going to be used to logging on the ASP.NET application.

How can I do this?

Thanks!

A: 
System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString
Kezzer
This won't work with ASP.NET applications, since this will return the credentials of IIS, not the logged in user
Lennaert
I use this everyday, and it works fine for me, and all the users using the website.
Kezzer
A: 

You should look into the active directory membership provider. It's built into ASP.NET.

Neil Barnwell
+1  A: 

Here is C# code I use to authenticate against the Active Directory

using System;
using System.DirectoryServices;

namespace XYZcompany.Enterprise
{
  public class AuthenicationMgr
  {
    private static readonly int AD_ERR_LOGON_FAIL = -2147023570;
    private static readonly string _path = "LDAP://xxx.yyy.ggg";
    private static readonly string _domain = "xxx.yyy.ggg";

    public static bool IsAuthenticated(string username, string pwd)
    {
      bool authenticatedFlag = true;
      string domainAndUsername = _domain + "\\" + username;
      DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
      try
      {
        // Bind to the native AdsObject to force authentication.
        Object obj = entry.NativeObject;
        DirectorySearcher search = new DirectorySearcher(entry);

        search.Filter = "(SAMAccountName=" + username + ")";
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

        if (result == null)
        {
          authenticatedFlag = false;
        }
        else
        {
          authenticatedFlag = true;
        }
      }
      catch (System.Runtime.InteropServices.COMException ex)
      {
        if (ex.ErrorCode == AD_ERR_LOGON_FAIL)
        {
          authenticatedFlag = false;
        }
        else
        {
          throw new ApplicationException("Unable to authenticate user due to system error.", ex);
        }
      }
      return authenticatedFlag;
    }
  }
}
StartClass0830
I don't ask this but I will need it. Thank you!
VansFannel
+1  A: 

For ASP.net, you can probably use

HttpContext.Current.User.Identity

If IIS is configured correctly (no anonymous logons, at least)

Lennaert
+3  A: 

In IIS, turn on Integrated Windows Authentication, and in code, if you use:

Request.ServerVariables["LOGON_USER"]

it will return the windows username of the logged in user, i.e. MYDOMAIN\MYUSERNAME

JonoW
If the user does anonymous logon on IIS I can't see his data. This only works on the computer where IIS is running.
VansFannel
Ok, It works with anonumous logon disable on IIS.
VansFannel
does anybody knows the java/tomcat equivalent???
opensas