在c#中为Microsoft Identity Manager生成唯一的电子邮件

时间:2016-11-03 10:42:57

标签: c# email identity

我有一个数据库广告Microsoft Identity Manager,用于生成从HR到MS Active Directory的用户帐户等等。

我有一个生成唯一电子邮件的代码:

case "mailgenerate":
                if (mventry["email"].IsPresent) 
                {
                    // Do nothing, the mail was already generated.
                }

                 {

                    if (csentry["FIRST"].IsPresent && csentry["LAST"].IsPresent);
                   {
                        string FirstName = replaceRUEN(csentry["FIRST"].Value);
                        string LastName = replaceRUEN(csentry["LAST"].Value);
                        string email = FirstName + "." + LastName + "@test.domain.com";
                         string newmail = GetCheckedMail(email, mventry);

                          if (newmail.Equals(""))
                        {
                            throw new TerminateRunException("A unique mail could not be found");
                        }
                          mventry["email"].Value = newmail;
                        }
                }
                break;


   //Generate mail Name method
    string GetCheckedMail(string email, MVEntry mventry)
    {
        MVEntry[] findResultList = null;
        string checkedmailName = email;
        for (int nameSuffix = 1; nameSuffix < 100; nameSuffix++)
        {
            //added ; and if corrected
            findResultList = Utils.FindMVEntries("email", checkedmailName,1);

            if (findResultList.Length == 0)
            {
                // The current mailName is not in use.
                return (checkedmailName);
            }
            MVEntry mvEntryFound = findResultList[0];
            if (mvEntryFound.Equals(mventry))
            {
                return (checkedmailName);
            }
            // If the passed email is already in use, then add an integer value
            // then verify if the new value exists. Repeat until a unique email is checked
            checkedmailName = checkedmailName + nameSuffix.ToString();
        }
        // Return an empty string if no unique mailnickName could be created.
        return "";
    }

问题: 当我第一次运行同步周期时,我得到正常的电子邮件 duplicateuser1@test.domain.com 对于下一个同步周期,此电子邮件将更新为 duplicateuser@test.domain.com1

这段代码我也用来生成mailnickname和accountname而没有任何问题。

有谁可以说为什么会这样? 谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于:

checkedmailName = checkedmailName + nameSuffix.ToString();

checkedmailName的值如下:firstName.lastName@test.domain.com

所以,你这样做:

checkedmailName = firstName.lastName@test.domain.com + 1;

你需要做这样的事情:

checkedmailName = checkedmailName.Split('@')[0] + nameSuffix.ToString()+ "@" + checkedmailName.Split('@')[1];

如果是这样,您将在@之前获得该部分,添加int值,然后附加@ + domain

由帖子作者更新我更改了分组 - &gt;拆分,它的工作原理。谢谢!