2013年10月30日 星期三

簡易AD驗証

簡易AD驗証


C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;

public partial class Test_tsLDAP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// Button1 Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string a;
            a = isADauth("user1", "pass1");
            Response.Write(a.ToString());
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            //throw ex;
        }
       
    }

    /// <summary>
    /// AD驗証
    /// </summary>
    /// <param name="uid">帳號</param>
    /// <param name="passwd">密碼</param>
    /// <returns></returns>
    public static string isADauth(string uid, string passwd)
    {
        string authentic = "";
        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://DC01", uid, passwd);
            object nativeObject = entry.NativeObject;
            authentic = "LOGIN_SUCCESSFUL";
        }
        catch (DirectoryServicesCOMException dscom_ex)
        {
            //authentic = "LOGIN_FAILED";
            throw dscom_ex;
        }
        return authentic;
    }
}

沒有留言:

張貼留言