C# openldap登入驗證並獲取多屬性
阿新 • • 發佈:2018-11-09
1. 設計介面輸入需要驗證的賬號密碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="ldap.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> username: <asp:TextBox ID="username" runat="server"></asp:TextBox> <br /> <br /> password: <asp:TextBox ID="password" type="password" runat="server" TextMode="Password"></asp:TextBox> <br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="login" /> <hr /> <br /> <asp:Label ID="MSG" runat="server" ForeColor="#CC6600"></asp:Label> <br /> <asp:Image ID="Image1" runat="server" /> </div> </form> </body> </html>
2. 點選登入後的處理程式
protected void Button1_Click(object sender, EventArgs e) { DirectoryEntry objUserEntry = new DirectoryEntry("LDAP://gdfi.com.cn:389/uid=" + username.Text + ",ou=teacher,dc=gdfi,dc=com,dc=cn", "uid=" + username.Text + ",ou=teacher,dc=gdfi,dc=com,dc=cn", password.Text, AuthenticationTypes.None); try { if (null != objUserEntry && objUserEntry.Properties.Count > 0) { MSG.Text = "uid:"+ objUserEntry.Properties["uid"].Value.ToString(); MSG.Text += "<br>email:" + objUserEntry.Properties["mail"].Value.ToString(); Session["img"] = (byte[])objUserEntry.Properties["jpegPhoto"].Value; Image1.ImageUrl = "readimg.ashx"; }else{ MSG.Text = "驗證失敗!"; } } catch (Exception){ MSG.Text = "驗證失敗!"; } }
3.其中,需要讀取照片,把照片資料儲存到session,再在另外一個readimg.ashx 讀出
public class readimg : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite((byte[])context.Session["img"]); } public bool IsReusable { get { return false; } } }
4.測試效果