????????? ???? ?? ?? ??? ?? ??? ???? ?? ???? ?? asp.NET ????????? ??????? ??????????? ?????? ????????? (LDAP) ?? ????? ?? ?????? ??????????? ?? ??????? ?????????? ???????????? ?? ?????? ???? ?? ??? ??????? ??????? ????? ?? ???? ???? ?????????? ?????????? ?? ?????????????? ??, ?? ??? ?? ????? ?? ???? ???
Application_AuthenticateRequest?????? ?? ???????? ???? ?? ??? Global.asax ????? ?? ??
GenericPrincipal??? ????????
HttpContext.User??? ?? ???? ?????? ?? ????? flows.
????? C# .NET ??? ?? asp.NET ??? ????????? ?????
??? ??? asp.NET ??? ????????? ??? ????? C# .NET FormsAuthAd ???? ????? ?? ???, ????? ????? ?? ???? ????:
- Microsoft Visual Studio .NET ???? ????..
- ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
- ????? ????,????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???ASP.NET ??? ??????????? ???????????????.
- ??????????????? ???, ?? ??? WebApplication1 ?????FormsAuthAd.
- ????? ????,OK.
- ????-????? ???????????????? Explorer, ?? ???? ??? ??? ????????? ??????.
- ????? ????.NET??? ????????? ??????????? ????? ???, ????? ????System.DirectoryServices.dll????? ????,??? ?????? ????-????? ????, ?? ???? ???OK.
??????? ??? ?????
????? ????? ?? ??? ?? ??? ???? LdapAuthentication.cs ??? ??? ????? ????? ?? ???? ????:
- ?????? Explorer ???, ????????? ??? ?? ????-????? ????, ?? ????? ????add?? ????-????? ????, ?? ???? ????? ???? ??? ?????.
- ????? ????,?????? ???????????????.
- ??????:LdapAuthentication.cs????????????? ???, ?? ???? ????????.
- ?????? ??? LdapAuthentication.cs ????? ??? ????? ??? ?? ???????????? ?????
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;
namespace FormsAuth
{
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;
public LdapAuthentication(String path)
{
_path = path;
}
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public String GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for(int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if(-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
??????? ??? ?????, ??? ?????????? ???, ???????, ?? ?????? ?????????? ??? ???? ?? ??? ??? ?? ??????? ???? ??? ?? ??? LDAP ?????????? ??????? ?? ????? ???? ???
Logon.aspx ??? ??? ??? ???
LdapAuthentication.IsAuthenticated???? ?? passes ?????????? ?? ???????? ??????????? ??? ??? ???, ??
DirectoryEntryobject is created with the path to the directory tree, the user
name, and the password. The user name must be in the "domain\username" format.
The
DirectoryEntryobject then tries to force the
AdsObjectbinding by obtaining the
NativeObject???? If this succeeds, the
CNattribute for the user is obtained by creating a
DirectorySearcherobject and by filtering on the
SAMAccountName. After the user is authenticated, the
IsAuthenticated???? ???? ??
True.
To obtain a list of groups that the user belongs to,
this code calls the
LdapAuthentication.GetGroups?????? ??? The
LdapAuthentication.GetGroups???? obtains ?? ?????????? ?? ????? ???? ??? ?? ??????? ?? ????? ?????? ?? ??? ???? ????
DirectorySearcher???????? ?? ?? ?????? ?? ??????? ????
memberOf??????? ??? ?? ???? ?? ?? ?????? (|) ?? ?????? ??? ?????? ?? ???? ???? ???
?? ?????
LdapAuthentication.GetGroups???? manipulates ?? ?????????? truncates ??? ?? ???????? ??????? ???? ??? ???????? ?? ????? ?? ?? ???? ??? ??? ??? ???????? ???? ???? ??, ???????? ???? ?? ?????? ????? ????????????
CN=...,...,DC=domain,DC=com
?? ?? ???? ???????? ??? ???? ???? ?? ???????? ?? ????? ???? ?? ????? ?? ???? ??, ??? ????????? ??????? ???? ?? ??????? ???? ?? ???? ??, ?? ?? ????? ????? ?? ?????????????? ????? ???????, ??? ?? ???? multi-domain ??????? ??? ???, ?? ?? ???? ?? ???? ?????? ?? ??? ????? ??? ?? ?? ???? ??? ?? ???? ??????? ????? ??? ???? ??? ?? ??? ???? ???? ?? ???? ?? ???? ?? ????? ??? differentiate ???? ?? ??? ????? ??? ?? ???? ?? ??? ???
??????? ????????? ?????? ?? 4096 ?????? ?? ?????? ???? ?? ???????? ??????? ???? ?? ????? ?? ???? ?? ???? ??, ??? ?? asp.NET ??? ???????? ?? ???? ??????? ??? ???? ??????? ?? ???????? ???? ?? ??? ?? ????? ???????? ??? ??, ?? ???? ?? ?? ???? ????? ?????????? ???? ?? ???? ???? ??? ??????? ????? ??? ?? ??????? ???????? ???? ?? ????
Global.asax ??? ?????
Global.asax ????? ??? ??? ?????? ????? ?? ??
Application_AuthenticateRequest????? ??????? ?? ????? ?????? retrieves ??????? ???? ??
Context.Request.Cookies??????, ???? decrypts, ?? ??? ???????? ???? ????? ?? ?????? ?? ???? retrieves
FormsAuthenticationTicket.UserData???? ???? ??? ?? ???? ???? ???? Logon.aspx ????? ??? ???? ?? ????? ???
??? ?????? ???????? ?? ????? ?? ??? ?? ???????? ???? ??? ??
GenericPrincipal???????? ??? ???? ???
GenericPrincipal???????? ????? ??? ??, ?? ???????? ??? ??? ??
HttpContext.User????
- ?????? Explorer ???, ???? ?????Global.asax?? ????-????? ????, ?? ???? ???????? ???.
- ??? behind Global.asax.cs ????? ?? ????? ?? ????? ??? ?? ??????:
using System.Web.Security;
using System.Security.Principal;
- ?????? ???? ????? ?????? ?? ??? ?????Application_AuthenticateRequest????? ??? ?? ????
void Application_AuthenticateRequest(Object sender, EventArgs e)
{
String cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if(null == authCookie)
{//There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{
//Write the exception to the Event Log.
return;
}
if(null == authTicket)
{//Cookie failed to decrypt.
return;
}
//When the ticket was created, the UserData property was assigned a
//pipe-delimited string of group names.
String[] groups = authTicket.UserData.Split(new char[]{'|'});
//Create an Identity.
GenericIdentity id = new GenericIdentity(authTicket.Name, "LdapAuthentication");
//This principal flows throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, groups);
Context.User = principal;
}
Web.config ????? ??????? ????
?? ?????? ??? ?? ????????
<forms></forms>,
<authentication></authentication>, ??
<authorization></authorization>Web.config ????? ??? ????? ?? ?????????? ?? ??? ???? ?????? ?????????? ????????? ?? ????? ???? ???, ?? ?? Logon.aspx ????? ?? ??????? ?????? ?????????????? ???? ?? ?? ??????????? ?? ???? ??? ???????????? ?? ?????? ?? ????? ????????? ?? ??? ?????? ???? ?? ??? ??????? ?? ???? ????
?????? ??? Web.config ????? ??? ????? ??? ?? ???????????? ?????
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="adAuthCookie" timeout="10" path="/" >
</forms>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<identity impersonate="true" />
</system.web>
</configuration>
?????
<identity impersonate="true"></identity>??????????? ????? ???? ???? asp.NET ???? ???? ?? Microsoft ??????? ?????????? ???????? (IIS) ?? ??? ??? ???????? ???? ??? ???? ?? ?????????? ???? ?? ??? ??? ?? ??? ??? ??? ?????? ?? ?? ??????????? ???, ?? ????????? ?? ??? ??? ?????? ????? ???????? ???? ??? ???? ?? ??????? ?????? ?? ???????? ?????????? ?????? ?????????? ?? ??????? ???????? ???? ?? ??? ??????????? ?????? ???? ??, ????? ?????? ?????????? ?? ??????? ?? ?? ???? ???????? ???? ??? ???? ??? ???? ??????? ?? ???, ?????
??????????
IIS ???? ??????? ?? ??? ???????? ????
???? ??????? ?? ??? IIS ?? ???????? ???? ?? ??? ????? ????? ?? ???? ????:
- IIS, ??? ???? ????? ?? ??? ???????? ??? ?? ??????? ????, ??????? ??????? ??????, ??????? ??????????? ??? ????, ????-????? ????FormsAuthAd?? ????-????? ????, ?? ???? ??????.
- ????? ?????????????? ??????? ????? ????-????? ????, ?? ???? ??????????? ??????????? ????? ?? ??????? ????????.
- ???? ?? ????????? ?? ??? ???? ???? ?? ?????? ?????????? ?? ?????? ???? ???? ???? ????
- ?????? ??? IIS ??? ???????? ??????? ???????? ?? ???? ???? ?? ??? ????? ?????
- ? ?????????? ????? ? ??? ??? ? ?????? Windows ?????????? ? ??? ????? ?? ????? ?????
- ??? ????? ?????
- ??? ??? ???? ????
??????? IUSR_
computername???? ?? ??? ?????? ?????????? ?? ?????? ???? ???
Logon.aspx ????? ?????
??? ??? asp.NET ??? ??????? Logon.aspx ???? ????? ?? ???, ????? ????? ?? ???? ????:
- ?????? Explorer ???, ????????? ??? ?? ????-????? ????, ?? ????? ????add?? ????-????? ????, ?? ???? ?????? ??????? ?????.
- ??????:Logon.aspx????????????? ???, ?? ???? ????????.
- ?????? Explorer ???, ???? ?????Logon.aspx?? ????-????? ????, ?? ???? ???????? ???????.
- ????? ????html??? ??????? ??? ???
- ?????? ??? ?? ????? ??? ?? ???????????? ?????
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>
<html>
<body>
<form id="Login" method="post" runat="server">
<asp:Label ID="Label1" Runat=server >Domain:</asp:Label>
<asp:TextBox ID="txtDomain" Runat=server ></asp:TextBox><br>
<asp:Label ID="Label2" Runat=server >Username:</asp:Label>
<asp:TextBox ID=txtUsername Runat=server ></asp:TextBox><br>
<asp:Label ID="Label3" Runat=server >Password:</asp:Label>
<asp:TextBox ID="txtPassword" Runat=server TextMode=Password></asp:TextBox><br>
<asp:Button ID="btnLogin" Runat=server Text="Login" OnClick="Login_Click"></asp:Button><br>
<asp:Label ID="errorLabel" Runat=server ForeColor=#ff3300></asp:Label><br>
<asp:CheckBox ID=chkPersist Runat=server Text="Persist Cookie" />
</form>
</body>
</html>
<script runat=server>
void Login_Click(Object sender, EventArgs e)
{
String adPath = "LDAP://corp.com"; //Fully-qualified Domain Name
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
String groups = adAuth.GetGroups();
//Create the ticket, and add the groups.
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, txtUsername.Text,
DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
//Encrypt the ticket.
String encryptedTicket = FormsAuthentication.Encrypt(authTicket);
//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if(true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
//You can redirect now.
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
}
else
{
errorLabel.Text = "Authentication did not succeed. Check user name and password.";
}
}
catch(Exception ex)
{
errorLabel.Text = "Error authenticating. " + ex.Message;
}
}
</script> - ???? LDAP ?????????? ????? ?? ????? ???? ?? ??? Logon.aspx ????? ??? ?? ?? ??????? ?????
Logon.aspx ????? ?? ???? ????? ?? ?? ?????????? ?? ??? ??????? ??? ?? ??????? ??????? ???? ??
LdapAuthentication????? ??? ?????????? authenticates ?? ???? ?? ?? ???? obtains ??, ?? ??? ??? ????? ?? ??
FormsAuthenticationTicket????????, ???? encrypts, ???? ???? ???????????? ???? ????? ??, ???? ?? ????? ??
HttpResponse.Cookies??????, ?? ???? ??? ??? ??? ?? ?????? ???? ??? URL ?? ??? ?????? ?????
WebForm1.aspx ????? ?? ??????? ????
WebForm1.aspx ????? ??? ??? ??? ?? ?????? ?? ?? ????? ???
?? ?????????? ?? ????? ?? ?????? ???? ??, ?? ?????? ???? ?? ??? Logon.aspx ?????????????? ?? ?????? ?????? ?????????? ??, ?? ??? ?????? WebForm1.aspx ????? ?? ??? ?????????????? ???
- ?????? Explorer ???, ???? ?????WebForm1.aspx?? ????-????? ????, ?? ???? ???????? ???????.
- ????? ????html??? ??????? ??? ???
- ?????? ??? ?? ????? ??? ?? ???????????? ?????
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Security.Principal" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label ID="lblName" Runat=server /><br>
<asp:Label ID="lblAuthType" Runat=server />
</form>
</body>
</html>
<script runat=server>
void Page_Load(Object sender, EventArgs e)
{
lblName.Text = "Hello " + Context.User.Identity.Name + ".";
lblAuthType.Text = "You were authenticated using " + Context.User.Identity.AuthenticationType + ".";
}
</script>
- ??? ?????? ?? ??????, ?? ?? ????????? ??????? ?????
- WebForm1.aspx ????? ?? ??? ?????? ????? ????? ??? ?? ?? Logon.aspx ???? ?? ??? ?????????????? ????
- ????? ??????????? ???? ????, ?? ???? ??? ????? ????????? ????. ?? ?? WebForm1.aspx ???? ?? ??? ?????????????? ???, ?? ????? ?? ???? ?????????? ??? ????? ?? ?? ??LdapAuthentication?? ??? ??????? ?????? ??Context.User.AuthenticationType????
???:Microsoft ??????? ???? ?? ?? ??????? ??????? ???? (SSL) ?? ??????????? ??????? ??????? ?? ????? ????? ?? ??????? ?????????? ??????? ???? ?? ?????? ?????? ??, ?? SSL ??????????? ?? ????????? ?? ??? ?? ??????? ???? ?? ??? ?? ???? ???????? ??????? ???????? ???? ?? ??? ?? ?? compromising ?? ????? ???
???? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ???????? ????? ????::
306590
(http://support.microsoft.com/kb/306590/
)
ASP.NET ??????? ????????? ??????
317012
(http://support.microsoft.com/kb/317012/
)
ASP.NET ??? ????????? ?? ?????? ?????
311495
(http://support.microsoft.com/kb/311495/
)
???????-?????? ?????????? ?? ??? ???-?????? ??????? ????? C# .NET ?? ????? ?? ???? asp.NET ????????? ??? ??????????? ???? ?? ??? ???? ????
313091
(http://support.microsoft.com/kb/313091/
)
??????? ?????????? ??? ????? ?? ??? Visual Basic .NET ?? ????? ?? ????? ?? ????? ?? ??? ???? ????
313116
(http://support.microsoft.com/kb/313116/
)
??????? ??????? ?????? loginUrl ????? ?? ??? ????????? ???? ???