Windows Authentication

Last post 04-19-2005, 10:45 AM by cutechat. 2 replies.
Sort Posts: Previous Next
  •  04-19-2005, 6:47 AM 5871

    Windows Authentication

    Hello all,
    Is there any way I can get Cute Chat to use Windows Authentication to allow users to sign in with their NT usernames/password?
     
    Thanks,
    Steve.
  •  04-19-2005, 10:42 AM 5887 in reply to 5871

    Re: Windows Authentication

    Yes , you can .
    You must implement two classes and register it using the CuteChatConfig.exe

    for the CuteSoft.Chat.UserAdapter.GetUserUniqueName()
    you could
    return System.Security.Principal.WindowsIdentity.GetCurrent().Name
     
    for the CuteSoft.Chat.DataProvider.ListUserUniqueName() and other methods 
     , you need to use the ActiveDirectory API to list the windows members .


    (I wrote one for the 1.1 before , here is the 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; } } }

    Today I am busy , please wait , I will write one two days later .
     
    Regards , Terry .
  •  04-19-2005, 10:45 AM 5888 in reply to 5887

    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 as RSS news feed in XML