书签-Bookmark
View Code
using System; using System.Activities; namespace DataService { public sealed class WaitForInput<T> : NativeActivity<T> { public WaitForInput() : base() { } public string BookmarkName { get; set; } public OutArgument<T> Input { get; set; } protected override void Execute(NativeActivityContext context) { context.CreateBookmark(BookmarkName, new BookmarkCallback(this.Continue)); } void Continue(NativeActivityContext context, Bookmark bookmark, object obj) { Input.Set(context, (T)obj); } protected override bool CanInduceIdle { get { return true; } } } }
Active Directory操作类-引用System.DirectoryServices
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.DirectoryServices; using System.Runtime.Serialization; namespace DataService { [DataContract] public class ADUserInfo { [DataMember] public string SamAccountName { get; set; } [DataMember] public string DisplayName { get; set; } [DataMember] public string Mail { get; set; } [DataMember] public string Company { get; set; } [DataMember] public string Department { get; set; } [DataMember] public string Title { get; set; } [DataMember] public string Manager { get; set; } } public class ADHelper { public const string path = "LDAP://192.168.1.195"; public const string domain = "sgsg"; public const string username = "oamaster"; public const string password = "oamaster@sgsg.cc"; public static bool IsAuthenticated(string path, string domain, string username, string pwd) { DirectoryEntry entry = new DirectoryEntry(path, domain + @"\" + username, pwd); try { DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne(); if (result == null) { return false; } } catch (Exception ex) { throw new Exception("Error authenticating user. " + ex.Message); } return true; } public static ADUserInfo GetUserInfo(string username) { ADUserInfo u = null; DirectoryEntry entry = new DirectoryEntry( ADHelper.path, ADHelper.domain + @"\" + ADHelper.username, ADHelper.password); try { DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.AddRange( new string[] { "samaccountname", "displayname", "mail", "company", "department", "title", "manager" }); SearchResult r = search.FindOne(); if (r != null) { u = new ADUserInfo() { Company = r.Properties.Contains("company") ? r.Properties["company"][0].ToString() : "", Department = r.Properties.Contains("department") ? r.Properties["department"][0].ToString() : "", DisplayName = r.Properties.Contains("displayname") ? r.Properties["displayname"][0].ToString() : "", Mail = r.Properties.Contains("mail") ? r.Properties["mail"][0].ToString() : "", Manager = r.Properties.Contains("manager") ? r.Properties["manager"][0].ToString().Split(',')[0].Replace("CN=", "") : "", SamAccountName = r.Properties.Contains("samaccountname") ? r.Properties["samaccountname"][0].ToString() : "", Title = r.Properties.Contains("title") ? r.Properties["title"][0].ToString() : "" }; } } catch{} return u; } public static List<ADUserInfo> GetADUserList() { var list = new List<ADUserInfo>(); var adRootDir = new DirectoryEntry(path, domain + @"\" + username, password); var ouRoot = "SGSG"; var ouRootDir = adRootDir.Children.Find("ou=" + ouRoot); var searcher = new DirectorySearcher(ouRootDir); searcher.Filter = "(objectClass=user)"; searcher.SearchScope = SearchScope.Subtree; searcher.Sort = new SortOption("name", System.DirectoryServices.SortDirection.Ascending); // If there is a large set to be return ser page size for a paged search searcher.PageSize = 512; /******************************************* SamAccountName-----------帐号 DisplayName ----------------显示名称 Mail---------------------------邮箱 Company---------------------公司 Department------------------部门 Title---------------------------职位 Manager---------------------经理 /*******************************************/ searcher.PropertiesToLoad.AddRange( new string[] { "samaccountname", "displayname", "mail", "company", "department", "title", "manager" }); SearchResultCollection results = searcher.FindAll(); foreach (SearchResult r in results) { list.Add(new ADUserInfo() { Company = r.Properties.Contains("company") ? r.Properties["company"][0].ToString() : "", Department = r.Properties.Contains("department") ?r.Properties["department"][0].ToString(): "", DisplayName = r.Properties.Contains("displayname") ?r.Properties["displayname"][0].ToString(): "", Mail = r.Properties.Contains("mail") ?r.Properties["mail"][0].ToString(): "", Manager = r.Properties.Contains("manager") ? r.Properties["manager"][0].ToString().Split(',')[0].Replace("CN=", "") : "", SamAccountName = r.Properties.Contains("samaccountname") ?r.Properties["samaccountname"][0].ToString(): "", Title = r.Properties.Contains("title") ? r.Properties["title"][0].ToString() : "" }); } return list; } public static SearchResultCollection GetOUMember(string path, string domain, string username, string password, string schemaClassNameToSearch, string ouName) { var adRootDir = new DirectoryEntry(path, domain + @"\" + username, password); var ouRoot = "SGSG"; var ouRootDir = adRootDir.Children.Find("ou=" + ouRoot); var ouDir = ouRootDir.Children.Find("ou=" + ouName); var searcher = new DirectorySearcher(ouDir); searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")"; searcher.SearchScope = SearchScope.Subtree; searcher.Sort = new SortOption("name", System.DirectoryServices.SortDirection.Ascending); // If there is a large set to be return ser page size for a paged search searcher.PageSize = 512; /******************************************* SamAccountName-----------帐号 DisplayName ----------------显示名称 Mail---------------------------邮箱 Company---------------------公司 Department------------------部门 Title---------------------------职位 Manager---------------------经理 /*******************************************/ searcher.PropertiesToLoad.AddRange( new string[] { "samaccountname", "displayname", "mail", "company", "department", "title", "manager" }); SearchResultCollection results = searcher.FindAll(); return results; } } }
Exchange Server操作类-引用邮件Web服务地址https://192.168.1.195/EWS/Services.wsdl
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using DataService.MailService; namespace DataService { public class MailHelper { public static bool SendEmail(string subject, string body, string mailTo, string ccTo = "") { try { CreateEmail("oamaster", "oamaster@sgsg.cc", "sgsg", "https://192.168.1.195/EWS/exchange.asmx", "oamaster@sgsg.cc", mailTo, subject, body, ccTo); return true; } catch { return false; } } private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } private static void CreateEmail(string userName, string passWord, string domain, string url, string mailFrom, string mailTo, string subject, string body, string ccTo = "") { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidationCallback; // Create service binding. // 建立service绑定 ExchangeServiceBinding esb = new ExchangeServiceBinding(); esb.Credentials = new NetworkCredential(userName, passWord, domain); esb.Url = url; // Create the CreateItem request. CreateItemType createItemRequest = new CreateItemType(); // Specifiy how the created items are handled // 如何处理邮件 createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy; createItemRequest.MessageDispositionSpecified = true; // Specify the location of sent items. createItemRequest.SavedItemFolderId = new TargetFolderIdType(); DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType(); sentitems.Id = DistinguishedFolderIdNameType.sentitems; createItemRequest.SavedItemFolderId.Item = sentitems; // Create the array of items. createItemRequest.Items = new NonEmptyArrayOfAllItemsType(); // Create a single e-mail message. // 新建一封邮件message对象 MessageType message = new MessageType(); message.Subject = subject; message.Body = new BodyType(); message.Body.BodyType1 = BodyTypeType.HTML; message.Body.Value = body; message.ItemClass = "IPM.Note"; message.Sender = new SingleRecipientType(); message.Sender.Item = new EmailAddressType(); message.Sender.Item.EmailAddress = mailFrom; //收件人 if (mailTo != "") { if (mailTo.IndexOf(";") >= 0) { var arr = mailTo.Split(';'); message.ToRecipients = new EmailAddressType[arr.Length]; for (int i = 0; i < arr.Length; i++) { message.ToRecipients[i] = new EmailAddressType(); message.ToRecipients[i].EmailAddress = arr[i]; } } else { message.ToRecipients = new EmailAddressType[1]; message.ToRecipients[0] = new EmailAddressType(); message.ToRecipients[0].EmailAddress = mailTo; } } //抄送 if (ccTo != "") { if (ccTo.IndexOf(";") >= 0) { var arr = ccTo.Split(';'); message.CcRecipients = new EmailAddressType[arr.Length]; for (int i = 0; i < arr.Length; i++) { message.CcRecipients[i] = new EmailAddressType(); message.CcRecipients[i].EmailAddress = arr[i]; } } else { message.CcRecipients = new EmailAddressType[1]; message.CcRecipients[0] = new EmailAddressType(); message.CcRecipients[0].EmailAddress = ccTo; } } message.Sensitivity = SensitivityChoicesType.Normal; // Add the message to the array of items to be created. createItemRequest.Items.Items = new ItemType[1]; createItemRequest.Items.Items[0] = message; try { // Send the request to create and send the e-mail item, and get the response. CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest); // Determine whether the request was a success. if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error) { throw new Exception(createItemResponse.ResponseMessages.Items[0].MessageText); } else { //Item was created } } catch (Exception ex) { throw ex; } } } }
WF操作类
View Code
//工作流持久化 private SqlWorkflowInstanceStore GetWorkflowInstanceStore() { var instanceStore = new SqlWorkflowInstanceStore(ConfigurationManager.AppSettings["db"]); var view = instanceStore.Execute (instanceStore.CreateInstanceHandle() , new CreateWorkflowOwnerCommand() , TimeSpan.FromSeconds(30)); instanceStore.DefaultInstanceOwner = view.InstanceOwner; return instanceStore; } //提交 public void MaterialCodeApplySubmit(ViewModels.ViewMaterialCodeApply entity) { var instanceStore = GetWorkflowInstanceStore(); var parameters = new Dictionary<string, object>(); parameters.Add("Instance", entity); var wfApp = new WorkflowApplication(new Workflows.WFMaterialCodeApply(), parameters); wfApp.InstanceStore = instanceStore; wfApp.PersistableIdle = (arg) => PersistableIdleAction.Unload; wfApp.Run(); } //修改 public void MaterialCodeApplyModify(Guid workflowID, string bookmark, ViewModels.ViewMaterialCodeApply instance) { var instanceStore = GetWorkflowInstanceStore(); var wfApp = new WorkflowApplication(new Workflows.WFMaterialCodeApply()); wfApp.InstanceStore = instanceStore; wfApp.PersistableIdle = (arg) => PersistableIdleAction.Unload; wfApp.Load(workflowID); wfApp.ResumeBookmark(bookmark, instance); wfApp.Run(); } //审批 public void MaterialCodeApplyApproval(Guid workflowID, string bookmark, WorkflowHistory approvalData) { var instanceStore = GetWorkflowInstanceStore(); var wfApp = new WorkflowApplication(new Workflows.WFMaterialCodeApply()); wfApp.InstanceStore = instanceStore; wfApp.PersistableIdle = (arg) => PersistableIdleAction.Unload; wfApp.Completed = (arg) => { var entity = db.MaterialCodeApply.First(x => x.WorkflowID == workflowID); entity.WorkflowStep = "Finished"; entity.WorkflowStepDesc = " 已完成"; db.SaveChanges(); }; wfApp.Load(workflowID); wfApp.ResumeBookmark(bookmark, approvalData); wfApp.Run(); }