ASP.NET C# AD驗證登入(使用LDAP,WinNT)

ASP.NET C# AD驗證登入(使用LDAP,WinNT)

Login.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<head runat="server">
    <title>登入頁面</title>
</head>
<body>
    <table align="center"><tr><td>
    <form id="form1" runat="server">
    <div>
  <asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx"
            OnAuthenticate="Login1_Authenticate" BackColor="#EFF3FB" BorderColor="#B5C7DE"
            BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
            Font-Size="12px" ForeColor="#333333">
            <TextBoxStyle Font-Size="0.8em" />
            <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
                BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            <TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em"
                ForeColor="White" />
  </asp:Login>
 
    </div>
    </form></td></tr>
</table>
</body>
</html>

Login.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.DirectoryServices;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Lin

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

    }
    public static string ValidateUser(string ComputerName, string UserName, string Password)
    {
        
string strPath;

        if (ComputerName.IndexOf(',') != -1)
            strPath = string.Format(@"LDAP://{0}", ComputerName);
        else
            strPath = string.Format(@"WinNT://{0}/{1}, user", ComputerName, UserName);

        DirectoryEntry entry = new DirectoryEntry(strPath, UserName, Password);

        try
        {
            string objectSid =
                  (new SecurityIdentifier((byte[])entry.Properties["objectSid"].Value, 0).Value);

            return objectSid;
        }
        catch// (DirectoryServicesCOMException)
        {
            return null;
        }
        finally
        {
            entry.Dispose();
        }
    }

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string strComputerName = "test.com.tw";   
        string strUserName = ((System.Web.UI.WebControls.Login)sender).UserName;
        string strPassword = ((System.Web.UI.WebControls.Login)sender).Password;

        string strValidateUser = ValidateUser(strComputerName, strUserName, strPassword);

        if (strValidateUser != null)
        {
            e.Authenticated = true;
        }
        else
        {
            e.Authenticated = false;
        }
    }

}

 資料來源:http://www.player.idv.tw/prog/index.php/ASP.NET%E7%9A%84Login%E6%A9%9F%E5%88%B6_(Active_Directory)

但最近發現一個問題,就是該如何限制是要登入網域群組裡面的帳號?

我將上面的程式稍微修改了一下,請參考下一篇

 

 

參考或是複製語法時,別忘了留個言喔 ^ ^ ~