私は ASP.Net 4.0 Web アプリケーションを開発しており、すべてのページへの匿名アクセスを許可する必要がありますが、Active Directory に裏打ちされたフォーム認証を追加して、ユーザーがログインしたときに追加の (特権) コンテンツを表示したいと考えています。これを行う方法の例を探してインターネットを精査しましたが、手ぶらで出てきました。
これは私がこれまでに持っているものですが、機能していないようです... ログインをクリックすると、メインページにリダイレクトされ、Cookies Manager+ を使用して Cookie が作成されたことを確認できますが、まだ表示されます匿名テンプレート。私はおそらくこれについてすべて間違っていると思います...それを機能させるために修正する方法はありますか、または私が取り組んでいるこのタイプの認証の例はありますか?
LdapAuthentication.cs
public class LadpAuthentication
{
private string _path;
private string _filterAttribute;
public LadpAuthentication( 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;
}
}
ログイン.aspx.cs
protected void Page_Load( object sender, EventArgs e )
{
if( null != Request["logout"] )
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
authCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(authCookie);
Response.Redirect("~/Default.aspx");
return;
}
string username = Request["username"];
string password = Request["password"];
if( username != null && password != null )
{
LadpAuthentication ldap = new LadpAuthentication(ConfigurationManager.AppSettings["LogonServer"]);
if( ldap.IsAuthenticated("mydomain", username, password) )
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(60), false, null);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(authCookie);
Response.Redirect("~/Default.aspx");
}
else
test.Text = "Invalid username and/or password.";
}
}
ログイン.aspx
<asp:Label ID="test" runat="server" />
<form action="<%= ResolveClientUrl("~/Login.aspx") %>" method="post">
<label for="username">Username</label><br />
<input type="text" id="username" name="username" /><br />
<label for="password">Password:</label><br />
<input type="password" id="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
Global.asax.cs void Application_AuthenticateRequest(オブジェクト送信者、EventArgs e) {
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if( null == authCookie )
return;
try {
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
GenericIdentity gid = new GenericIdentity(authTicket.Name, "LdapAuthentication");
Context.User = new GenericPrincipal(gid,null);
} catch( Exception ex ) {
}
}
デフォルト.aspx
<asp:LoginView runat="server">
<AnonymousTemplate>
<a id="login-button" href="<%= ResolveClientUrl("~/Login.aspx") %>" class="ui-button">Login</a>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:LoginName runat="server" />
</LoggedInTemplate>
</asp:LoginView>