ASP.NET-AD(ActiveDirectory)用户验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET-AD(ActiveDirectory)用户验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5303字,纯文字阅读大概需要8分钟。
内容图文
http://blog.sina.com.cn/s/blog_5067ef490101e1ul.html
protected void Page_Load(object sender, EventArgs e) { // 認証処理開始 string staffCode = string.Empty; if (!CheckUtility.IsEmpty(Request.QueryString[ConstValue.QUERY_STRING_KEY_STFCD])) { staffCode = Request.QueryString[ConstValue.QUERY_STRING_KEY_STFCD].ToString(); } string loginUserID = string.Empty; if (CheckUtility.IsEmpty(staffCode)) { // URLパラメータで担当者コードが取得できなかったとき // ADよりドメイン情報の取得 string[] name = User.Identity.Name.Split(‘\\‘); // ADよりログインユーザのドメイン情報を取得 string loginUserDomain = name[0]; WebLogUtility.WriteDebugLog("ログインアカウント(ドメイン):" + loginUserDomain); // ADよりログインユーザIDを取得 loginUserID = name[1]; WebLogUtility.WriteDebugLog("ログインアカウント:" + loginUserID); LdapAuthentication ldap = new LdapAuthentication(); staffCode = ldap.GetEmployee(loginUserDomain, loginUserID); } //担当者コード(AD取得)で権限マスタから権限コードを取得 Dictionary<string, object> parameter = new Dictionary<string, object>(); parameter.Add(StaffInfoBll.REQUEST_KEY_STAFF_CODE, staffCode); ResponseDataType response = new StaffInfoBll().Execute(parameter); // BLLからの返却値を元にSessionに値を設定 StaffInfo staffInfo = (StaffInfo)response[StaffInfoBll.RESPONSE_KEY_STAFF_INFO]; if (staffInfo == null || CheckUtility.IsEmpty(staffInfo.StfCd)) { ApplException excep = new ApplException("担当者マスタにユーザーが存在しません。"); excep.LongInUserId = staffCode; excep.NtAccount = loginUserID; throw excep; } staffInfo.NtUserCd = loginUserID; staffInfo.Ip = GetClientIPAddress(); Session[ConstValue.SESSION_KEY_STAFF_INFO] = staffInfo; Session[ConstValue.SESSION_KEY_SYS_STAFF_INFO] = staffInfo; Session[ConstValue.SESSION_KEY_IS_AUTHENTICATED] = true; //} StringBuilder redirectUrl = new StringBuilder(); Dictionary<string, object> redirectKey = new Dictionary<string, object>(); // 遷移先URLの取得 string dispId = (string)Request.QueryString[ConstValue.QUERY_STRING_KEY_DISP_ID]; redirectUrl.Append(this.GetUrl(dispId)); redirectUrl.Append("?"); redirectUrl.Append(Request.QueryString.ToString()); //セッション変数への格納 Session.Add(ConstValue.SESSION_KEY_REDIRECT, redirectKey); // リダイレクト Response.Redirect(redirectUrl.ToString()); }
/// <summary> /// IPアドレスの取得 /// </summary> /// <returns></returns> public string GetClientIPAddress() { return Request.ServerVariables["REMOTE_ADDR"]; }
using System; using System.DirectoryServices; using System.Configuration; using Otsuka.Application.Common.Exception; using Otsuka.Application.Common; /// <summary> /// ActiveDirectoryのユーザ情報を取得する /// </summary> public class LdapAuthentication { /// <summary> /// /// </summary> private DirectoryEntry DrEntry; /// <summary> /// ドメイン名 /// </summary> private string _domainName; /// <summary> /// ユーザ /// </summary> private string _userName; /// <summary> /// パスワード /// </summary> private string _password; /// <summary> /// サーバ /// </summary> private string _serverName; // 第1引き数に、ユーザドメイン名を追加 /// <summary> /// ユーザの所属するドメイン名 /// </summary> private string _userDomainName; /// <summary> /// コンストラクタ /// </summary> public LdapAuthentication() { } /// <summary> /// 接続したディレクトリのユーザ情報を取得 /// </summary> /// <param name="domainName"> 担当者のドメイン名 </param> /// <param name="account"> 取得したい担当者のアカウント </param> /// <returns> 担当者コード </returns> public string GetEmployee(string domainName, string account) { this._domainName = ConfigurationManager.AppSettings[ConstValue.DC_DomainName].ToString(); this._userName = ConfigurationManager.AppSettings[ConstValue.DC_UserName].ToString(); this._password = ConfigurationManager.AppSettings[ConstValue.DC_Password].ToString(); this._serverName = ConfigurationManager.AppSettings[ConstValue.DC_ServerName].ToString(); //Web.Configから担当者のドメインと一致するLDAP_DNを取得if (ConfigurationManager.AppSettings["DC_LdapDn_" + domainName.ToLower()] != null) { _userDomainName = ConfigurationManager.AppSettings["DC_LdapDn_" + domainName.ToLower()].ToString(); _userDomainName = _userDomainName.ToLower(); } //AD接続確認if (!AccessAD()) { thrownew UserNotFoundException("", "", _userName); } //ユーザ情報取得 DirectorySearcher searcher = new DirectorySearcher(); searcher.SearchRoot = DrEntry; searcher.Filter = "(SAMAccountName=" + account + ")"; SearchResult result = searcher.FindOne(); // アカウントが存在しない場合 // アカウントのイニシャル項目に値が設定されていない場合if (result == null || result.Properties["Initials"].Count.Equals(0)) { thrownew UserNotFoundException("", "", account); } return result.Properties["Initials"][0].ToString(); } ///<summary>/// ディレクトリへの接続確認 ///</summary>///<returns>true:接続可能、false:接続不可</returns>privatebool AccessAD() { // アクセスするための情報を作成string domainAndUsername = _domainName + @"\" + _userName; string[] servers = _serverName.Split(‘,‘); foreach (string server in servers) { string LDAP = "LDAP://" + server; //====== 2010/02/26 [CLドメイン対応] ADD START ========= //呼出字にユーザドメイン名を追加if (!String.IsNullOrEmpty(_userDomainName)) { LDAP = LDAP + "/" + _userDomainName; } //====== 2010/02/26 [CLドメイン対応] ADD END ========= DrEntry = new DirectoryEntry(LDAP, domainAndUsername, _password); try { object navi = DrEntry.NativeObject; returntrue; } catch { continue; } } returnfalse; } }
原文:http://www.cnblogs.com/haiy/p/4142216.html
内容总结
以上是互联网集市为您收集整理的ASP.NET-AD(ActiveDirectory)用户验证全部内容,希望文章能够帮你解决ASP.NET-AD(ActiveDirectory)用户验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。