2013年10月30日 星期三

簡易AD驗証

簡易AD驗証


C#:

  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Web;
  8.  
  9. using System.Web.UI;
  10.  
  11. using System.Web.UI.WebControls;
  12.  
  13. using System.DirectoryServices;
  14.  
  15.  
  16.  
  17. public partial class Test_tsLDAP : System.Web.UI.Page
  18.  
  19. {
  20.  
  21.     protected void Page_Load(object sender, EventArgs e)
  22.  
  23.     {
  24.  
  25.  
  26.  
  27.     }
  28.  
  29.     /// <summary>
  30.  
  31.     /// Button1 Event
  32.  
  33.     /// </summary>
  34.  
  35.     /// <param name="sender"></param>
  36.  
  37.     /// <param name="e"></param>
  38.  
  39.     protected void Button1_Click(object sender, EventArgs e)
  40.  
  41.     {
  42.  
  43.         try
  44.  
  45.         {
  46.  
  47.             string a;
  48.  
  49.             a = isADauth("user1", "pass1");
  50.  
  51.             Response.Write(a.ToString());
  52.  
  53.         }
  54.  
  55.         catch (Exception ex)
  56.  
  57.         {
  58.  
  59.             Response.Write(ex.Message);
  60.  
  61.             //throw ex;
  62.  
  63.         }
  64.  
  65.        
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     /// <summary>
  72.  
  73.     /// AD驗証
  74.  
  75.     /// </summary>
  76.  
  77.     /// <param name="uid">帳號</param>
  78.  
  79.     /// <param name="passwd">密碼</param>
  80.  
  81.     /// <returns></returns>
  82.  
  83.     public static string isADauth(string uid, string passwd)
  84.  
  85.     {
  86.  
  87.         string authentic = "";
  88.  
  89.         try
  90.  
  91.         {
  92.  
  93.             DirectoryEntry entry = new DirectoryEntry("LDAP://DC01", uid, passwd);
  94.  
  95.             object nativeObject = entry.NativeObject;
  96.  
  97.             authentic = "LOGIN_SUCCESSFUL";
  98.  
  99.         }
  100.  
  101.         catch (DirectoryServicesCOMException dscom_ex)
  102.  
  103.         {
  104.  
  105.             //authentic = "LOGIN_FAILED";
  106.  
  107.             throw dscom_ex;
  108.  
  109.         }
  110.  
  111.         return authentic;
  112.  
  113.     }
  114.  
  115. }
  116.  

沒有留言:

張貼留言