在c#中为微软身份管理器生成唯一的电子邮件

本文关键字:唯一 电子邮件 管理器 身份 微软 | 更新日期: 2023-09-27 18:16:02

我有一个DB和Microsoft身份管理器从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,没有任何问题。

有谁能说说为什么会这样吗?谢谢!

在c#中为微软身份管理器生成唯一的电子邮件

问题出在这一行:

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


由线程作者更新,我更改了split -> split并且它有效。谢谢!