Re: Windows Authentication

  •  04-19-2005, 10:45 AM

    Re: Windows Authentication

    Code

     namespace CuteChat.WinAuthLib
    {
     using System;
     using System.Collections;
     using System.Collections.Specialized;
     using System.Configuration;
     using System.Data;
     using System.Data.SqlClient;

     using System.DirectoryServices;

     using CuteChat;
     using CuteChat.SqlServer;
     
     public class WinAuthDataProvider : SqlDataProvider
     {
      public WinAuthDataProvider(string connectionstring):base(connectionstring)
      {
      }

      private DirectoryEntry OpenPath()
      {
       NameValueCollection appSettings=ConfigurationSettings.AppSettings;

       //WinNT: case sensitive
       //"WinNT://." , WinNT://DOMAIN/SERVER , WinNT://WORKGROUP/PCNAME
       
       string path=appSettings["WinAuthPath"];
       
       string username=appSettings["WinAuthUsername"];
       string password=appSettings["WinAuthPassword"];
       
       if(path==null)path="WinNT://.";//local

       if(username==null)
        return new DirectoryEntry(path);

       return new DirectoryEntry(path,username,password);
      }

      public override string[] ListUserUniqueName()
      {
       ArrayList names=new ArrayList();

       using(DirectoryEntry nt=OpenPath())
       {
        foreach(DirectoryEntry child in nt.Children)
        {
         if(child.SchemaClassName=="User")
         {
          names.Add(child.Properties["Name"].Value);
         }
        }
       }

       return (string[])names.ToArray(typeof(string));
      }

      public override UserInfoData GetUserInfo(string useruniquename)
      {
       using(DirectoryEntry nt=OpenPath())
       {
        foreach(DirectoryEntry child in nt.Children)
        {
         if(child.SchemaClassName=="User")
         {
          string name=Convert.ToString(child.Properties["Name"].Value);
          if(string.Compare(name,useruniquename,true)==0)
          {
           bool isadmin=string.Compare(useruniquename,ConfigurationSettings.AppSettings["AdminLoginName"],true)==0;

           string fullname=Convert.ToString(child.Properties["FullName"].Value);
           if(fullname==null||fullname.Length==0)
            fullname=name;

           return new UserInfoData(useruniquename,fullname,isadmin);
          }
         }
        }
       }
       throw(new Exception("User not found"));
      }

      public override bool IsUserNickNameExists(string nickname)
      {
       using(DirectoryEntry nt=OpenPath())
       {
        foreach(DirectoryEntry child in nt.Children)
        {
         if(child.SchemaClassName=="User")
         {
          string name=Convert.ToString(child.Properties["Name"].Value);
          string full=Convert.ToString(child.Properties["FullName"].Value);
          if(string.Compare(name,nickname,true)==0)
           return true;
          if(string.Compare(full,nickname,true)==0)
           return true;
         }
        }
       }
       return false;
      }

     }
    }

View Complete Thread