public string getSamAcoutName(People person) { var _context = new ApplicationDbContext(); var personuser = _context.CustomUsers.FirstOrDefault(x => x.PeopleId == person.Id); if (personuser != null) { return(personuser.UserPrincipalName.Split('@')[0]); } // First attempt var SAN = hanaval.CleanText(person.Names).ToCharArray()[0].ToString() + "." + hanaval.CleanText(person.FirstSurName) + (!person.SecondSurName.IsNullOrWhiteSpace() ? ("." + hanaval.CleanText(person.SecondSurName).ToCharArray()[0].ToString()) : ""); SAN = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(SAN.Replace(" ", ""))); var UPN = SAN + "@" + sDomain; var search = _context.CustomUsers.Where(x => x.UserPrincipalName == UPN).ToList(); if (search.Any()) { // Second attempt SAN = hanaval.CleanText(person.Names).ToCharArray()[0].ToString() + hanaval.CleanText(person.Names).ToCharArray()[1].ToString() + "." + hanaval.CleanText(person.FirstSurName) + (!person.SecondSurName.IsNullOrWhiteSpace() ? ("." + hanaval.CleanText(person.SecondSurName).ToCharArray()[0].ToString()) : ""); SAN = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(SAN.Replace(" ", ""))); UPN = SAN + "@" + sDomain; search = _context.CustomUsers.Where(x => x.UserPrincipalName == UPN).ToList(); if (search.Any()) { // Third attempt SAN = hanaval.CleanText(person.Names).ToCharArray()[0].ToString() + hanaval.CleanText(person.Names).ToCharArray()[1].ToString() + "." + hanaval.CleanText(person.FirstSurName) + (!person.SecondSurName.IsNullOrWhiteSpace() ? ("." + hanaval.CleanText(person.SecondSurName).ToCharArray()[0].ToString() + hanaval.CleanText(person.SecondSurName).ToCharArray()[1].ToString()) : ""); SAN = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(SAN.Replace(" ", ""))); UPN = SAN + "@" + sDomain; search = _context.CustomUsers.Where(x => x.UserPrincipalName == UPN).ToList(); if (search.Any()) { // Fourth attempt SAN = hanaval.CleanText(person.Names).ToCharArray()[0].ToString() + "." + hanaval.CleanText(person.FirstSurName) + (!person.SecondSurName.IsNullOrWhiteSpace() ? ("." + hanaval.CleanText(person.SecondSurName).ToCharArray()[0].ToString()) : "") + (person.BirthDate.Day).ToString().PadLeft(2, '0'); SAN = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(SAN.Replace(" ", ""))); UPN = SAN + "@" + sDomain; Random rnd = new Random(); while (_context.CustomUsers.Where(x => x.UserPrincipalName == UPN).ToList().Any()) { // Last and final attempt SAN = hanaval.CleanText(person.Names).ToCharArray()[0].ToString() + "." + hanaval.CleanText(person.FirstSurName) + (!person.SecondSurName.IsNullOrWhiteSpace() ? ("." + hanaval.CleanText(person.SecondSurName).ToCharArray()[0].ToString()) : "") + (rnd.Next(1, 100)).ToString().PadLeft(2, '0'); SAN = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(SAN.Replace(" ", ""))); } } } } //person.UserPrincipalName = UPN; var newUser = new CustomUser(); newUser.Id = CustomUser.GetNextId(_context); newUser.PeopleId = person.Id; newUser.UserPrincipalName = UPN; _context.CustomUsers.Add(newUser); _context.SaveChanges(); return(SAN); }