出自ProgWiki
用途
程式碼
Web.Config
<?xml version="1.0"?>
<!--
注意: 除了手動編輯這個檔案以外,您也可以使用
Web 管理工具設定您的應用程式設定值。請使用
Visual Studio 中的 [網站] -> [ASP.NET 組態] 選項。
如需完整的設定與註解清單,請參考
machine.config.comments (通常位於
\Windows\Microsoft.Net\Framework\v2.x\Config)
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
設定 compilation debug="true" 會將偵錯
符號插入編譯過的頁面。因為這樣會
影響效能,所以只有在開發期間才能將
這個值設定為 true。
-->
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider" />
</providers>
</membership>
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
<!--
<authentication> 區段可以用來設定 ASP.NET
使用的安全性驗證模式,以識別連入的
使用者。
-->
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="login.aspx"
defaultUrl="default.aspx" protection="All" timeout="30" path="/"
requireSSL="false" slidingExpiration="true"
cookieless="UseDeviceProfile" domain=""
enableCrossAppRedirects="false">
<credentials passwordFormat="SHA1" />
</forms>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<!--
<customErrors> 區段可以用來設定
在執行要求期間發生未處理
錯誤時所要執行的動作。具體來說,
它可以讓開發人員設定要顯示的 HTML 錯誤網頁,
以取代錯誤堆疊追蹤。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>登入用頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx" OnAuthenticate="Login1_Authenticate">
</asp:Login>
</div>
</form>
</body>
</html>
Login.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.Security.Principal;
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)
{
// 如果 ping 不到 host name
// 要加 ip 與 host name 對應
// C:\WINDOWS\system32\drivers\etc\hosts.
// 例: 10.101.107.150 lab_evta.lab.corp
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 = "lab_evta.lab.corp"; //"Lab_evta";
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;
}
}
}
測試用
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Login.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strText = "";
if (this.Page.User.Identity.IsAuthenticated)
strText = "IsLogin = Yes<br/>";
else
strText = "IsLogin = No<br/>";
strText += "Name = " +this.Page.User.Identity.Name;
Label1.Text = strText;
}
}
相關