/// <summary> /// 编辑AD中已经注册的当前用户信息 /// </summary> /// <param name="loginName">当前用户的登录名</param> /// <param name="displayName">当前用户的显示名</param> /// <param name="email">当前用户的电子邮件</param> /// <param name="mobile">当前用户的手机号码</param> /// <returns>T/F</returns> public static bool EditUser(string loginName, string displayName, string email, string mobile) { DirectoryEntry currentUser = AdHelper.GetDirectoryEntryByAccount(loginName);//当前被编辑的用户 currentUser.Properties["displayName"][0] = displayName; //currentUser.Properties["name"][0] = displayName;//执行此句会出现错误 if (mobile != "") { if (currentUser.Properties.Contains("telephoneNumber")) { currentUser.Properties["telephoneNumber"][0] = mobile; } else { currentUser.Properties["telephoneNumber"].Add(mobile);//家庭电话otherTelephone } } if (email != "") { if (currentUser.Properties.Contains("mail")) { currentUser.Properties["mail"][0] = email; } else { currentUser.Properties["mail"].Add(email); } } if (mobile != "") { if (currentUser.Properties.Contains("mobile")) { currentUser.Properties["mobile"][0] = mobile; } else { currentUser.Properties["mobile"].Add(mobile);//手机号码 } } try { currentUser.CommitChanges(); return(true); } catch (Exception ex) { return(false); } }
/// <summary> /// 获取当前用户ID,如果数据库中没有数据,根据活动目录写入数据库中 /// </summary> /// <returns>mUser.UserID</returns> public static long GetUserId() { string account = GetAccount(); var db = new ProjectEntities();//定义数据库连接接口 ProjectDll.User mUser = db.User.FirstOrDefault(u => u.Account.Equals(account)); if (mUser != null) { return(mUser.UserID); } else { mUser = new ProjectDll.User(); mUser.Account = account; mUser.Created = DateTime.Now; SPSecurity.RunWithElevatedPrivileges(delegate() { DirectoryEntry adUser = AdHelper.GetDirectoryEntryByAccount(account); if (adUser != null) { mUser.Name = adUser.Properties["displayName"][0].ToString(); if (adUser.Properties.Contains("telephoneNumber")) { mUser.Telephone = adUser.Properties["telephoneNumber"][0].ToString(); } if (adUser.Properties.Contains("mail")) { mUser.Email = adUser.Properties["mail"][0].ToString(); } } }); db.User.Add(mUser); db.SaveChanges(); return(mUser.UserID); } }