如何使用Membership进行AD用户登陆验证
我是照着MSDN做的,网址:http://msdn.microsoft.com/zh-cn/library/ms998347.aspx使用Membership.ValidateUser()方法,返回的false,使用断点也进入不到Membership类调试,下面是我的代码一共有三个页面WebForm,一个Login.aspx(登陆页面),Info.aspx(登陆成功跳转到这个页面),Default.aspx(注销页面)
Login.aspx
C# code?12345678910111213141516 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebApplication1.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:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div> </form> </body> </html>
Login.aspx.cs
C# code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; 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.Linq; namespace WebApplication1 { public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //点击登陆按钮 //这里假设已经通过了数据库的对比,确实存在该用户 string userId = "Test\\admin"; string pa = "123456"; string roles = "Administrators"; //从其他地方取得用户角色数据 if (Membership.ValidateUser(userId, pa)) { FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userId, DateTime.Now, DateTime.Now.AddMinutes(1), true, roles); //建立身份验证票对象 string HashTicket = FormsAuthentication.Encrypt(Ticket); //加密序列化验证票为字符串 HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket); //生成Cookie Context.Response.Cookies.Add(UserCookie); //票据写入Cookie Response.Redirect("Info.aspx"); } else { Response.Write("无效用户名或密码!"); } //为用户名创建一个身份验证票据,并将其添加到响应的Cookie中 //以后用户验证都通过这个cookie来维持 //SetAuthCookie的第一个参数为已验证的用户的名称,一般就是用户id //SetAuthCookie的第二个参数为true时代表创建持久Cookie(跨浏览器会话保存的 Cookie) //为false则关闭浏览器后要重新验证身份 //FormsAuthentication.RedirectFromLoginPage(userId, false); } } }
Info.aspx
C# code?1234567891011121314151617 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Info.aspx.cs" Inherits="WebApplication1.Info" %> <!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> </div> </form> </body> </html>
Info.aspx.cs
C# code?123456789101112131415161718192021222324 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; 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.Linq; namespace WebApplication1 { public partial class Info : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write(User.Identity.IsAuthenticated +" "+ User.Identity.Name); } } }
Default.aspx
C# code?123456789101112131415161718 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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:Button Text="注销用户" ID="btnLogout" runat="server" onclick="btnLogout_Click"/> </div> </form> </body> </html>
Default.aspx.cs
C# code?1234567891011121314151617181920212223242526272829 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; 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.Linq; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogout_Click(object sender, EventArgs e) { FormsAuthentication.SignOut(); } } }
Web.config
C# code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 <?xml version="1.0"?> <configuration> <appSettings/> <connectionStrings> <add name="ADConnectionString" connectionString="LDAP://192.168.210.123/CN=Users,DC=test,DC=com"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0"/> <authentication mode="Forms"> <forms loginUrl="Login.aspx" protection="All" timeout="30" name="AppNameCookie" path="/FormsAuth" requireSSL="false" slidingExpiration="true" defaultUrl="Default.aspx" cookieless="UseCookies" enableCrossAppRedirects="false"/> </authentication> <authorization> <deny users="?" /> <allow users="*" /> </authorization> <membership defaultProvider="MembershipADProvider"> <providers> <add name="MembershipADProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" connectionUsername="test\admin" connectionPassword="123456"/> </providers> </membership> <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> </system.web> <!-- 用户角色授权 --> <location path="Info.aspx"> <system.web> <authorization> <allow roles="Administrator"/> <deny users="*"/> </authorization> </system.web> </location> </configuration>
C# .Net Asp.Net AD Active Directory --------------------编程问答-------------------- 在没有设置Membership的情况下,你登录AD验证成功吗? --------------------编程问答-------------------- 怎么设置Membership,不是在web.config里设置吗?
<membership defaultProvider="MembershipADProvider"> <providers>
<add name="MembershipADProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" connectionUsername="test\admin" connectionPassword="123456"/>
</providers>
</membership>
补充:.NET技术 , C#