)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Skip Navigation LinksHome » Code Library » NewIdentity

Public general use code classes and xml files that we've compiled and used over the years:

ASP.NET Identity support class.

    1: // for NewIdentity
    2: using Microsoft.AspNet.Identity;
    3: using System;
    4: using System.Collections.Generic;
    5: using Microsoft.EntityFrameworkCore;
    6: using System.Linq;
    7: using System.Web;
    8: using System.Web.Security;
    9:  
   10: namespace Ia.Cl.Model
   11: {
   12:     /*
   13:     ////////////////////////////////////////////////////////////////////////////
   14: 
   15:     /// <summary publish="true">
   16:     /// ASP.NET Identity support class.
   17:     /// </summary>
   18:     /// <value>
   19:     /// Identity: http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity
   20:     /// </value>
   21:     /// <remarks> 
   22:     /// Copyright © 2001-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   23:     ///
   24:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   25:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   26:     ///
   27:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   28:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   29:     /// 
   30:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   31:     /// 
   32:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   33:     /// </remarks> 
   34:     public class NewIdentity
   35:     {
   36:         private const int initialUserListLength = 1000;
   37:         //private static Dictionary<Guid, string> userList;
   38:         private static List<IdentityUser> identityUserList;
   39: 
   40:         ////////////////////////////////////////////////////////////////////////////
   41: 
   42:         /// <summary>
   43:         ///
   44:         /// </summary>
   45:         public static IdentityUser CreateUser(string userName, string password, string email, Microsoft.EntityFrameworkCore.DbContext context, out IdentityResult identityResult)
   46:         {
   47:             return CreateUser(userName, password, email, null, null, context, out identityResult);
   48:         }
   49: 
   50:         ////////////////////////////////////////////////////////////////////////////
   51: 
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         public static IdentityUser CreateUserWithNoEmailNoPassword(string userName, Microsoft.EntityFrameworkCore.DbContext context, out IdentityResult identityResult)
   56:         {
   57:             return CreateUser(userName, null, null, null, null, context, out identityResult);
   58:         }
   59: 
   60:         ////////////////////////////////////////////////////////////////////////////
   61: 
   62:         /// <summary>
   63:         ///
   64:         /// </summary>
   65:         public static IdentityUser CreateUser(string userName, string password, string email, string question, string answer, Microsoft.EntityFrameworkCore.DbContext context, out IdentityResult identityResult)
   66:         {
   67:             // Default UserStore constructor uses the default connection string named: DefaultConnection
   68:             IdentityUser identityUser;
   69:             UserStore<IdentityUser> userStore;
   70:             UserManager<IdentityUser> userManager;
   71: 
   72:             userStore = null;// new UserStore<IdentityUser>(context);
   73:             userManager = new UserManager<IdentityUser>(userStore);
   74: 
   75:             identityUser = new IdentityUser();
   76: 
   77:             identityUser.UserName = userName;
   78:             if (email != null) identityUser.Email = email;
   79: 
   80:             identityResult = (password != null) ? userManager.Create(identityUser, password) : userManager.Create(identityUser);
   81: 
   82:             return identityUser;
   83:         }
   84: 
   85:         /*
   86:         ////////////////////////////////////////////////////////////////////////////
   87: 
   88:         /// <summary>
   89:         ///
   90:         /// </summary>
   91:         public static MembershipUserCollection GetAllUsers(DbContext context)
   92:         {
   93:             MembershipUserCollection memberList;
   94: 
   95:             memberList = Membership.GetAllUsers();
   96: 
   97:             return memberList;
   98:         }
   99:          * /
  100: 
  101:         ////////////////////////////////////////////////////////////////////////////
  102: 
  103:         /// <summary>
  104:         ///
  105:         /// </summary>
  106:         public static IdentityUser IdentityUser(string userName, Microsoft.EntityFrameworkCore.DbContext context)
  107:         {
  108:             UserStore<IdentityUser> userStore;
  109:             IdentityUser identityUser;
  110: 
  111:             userStore = null;// new UserStore<IdentityUser>(context);
  112: 
  113:             identityUser = (from q in userStore.Users where q.UserName == userName select q).SingleOrDefault();
  114: 
  115:             return identityUser;
  116:         }
  117: 
  118:         ////////////////////////////////////////////////////////////////////////////
  119: 
  120:         /// <summary>
  121:         ///
  122:         /// </summary>
  123:         public static List<IdentityUser> IdentityUserList(Microsoft.EntityFrameworkCore.DbContext context)
  124:         {
  125:             UserStore<IdentityUser> userStore;
  126:             //UserManager<IdentityUser> userManager;
  127: 
  128:             userStore = null;// new UserStore<IdentityUser>(context);
  129:             //userManager = new UserManager<IdentityUser>(userStore);
  130: 
  131:             identityUserList = userStore.Users.ToList();
  132: 
  133:             return identityUserList.ToList();
  134:         }
  135: 
  136:         /*
  137:         ////////////////////////////////////////////////////////////////////////////
  138: 
  139:         /// <summary>
  140:         ///
  141:         /// </summary>
  142:         public static Dictionary<Guid, string> UserList(DbContext context)
  143:         {
  144:                 MembershipUserCollection membershipUserList;
  145: 
  146:                 if (userList == null || userList.Count == 0)
  147:                 {
  148:                     membershipUserList = GetAllUsers(context);
  149: 
  150:                     userList = new Dictionary<Guid, string>(membershipUserList.Count);
  151: 
  152:                     userList.Add(Guid.Empty, "");
  153: 
  154:                     foreach(MembershipUser mu in membershipUserList)
  155:                     {
  156:                         userList.Add(Guid.Parse(mu.ProviderUserKey.ToString()), mu.UserName);
  157:                     }
  158:                 }
  159: 
  160:                 return userList.ToList();
  161:         }
  162:          * /
  163: 
  164:         /*
  165:         ////////////////////////////////////////////////////////////////////////////
  166: 
  167:         /// <summary>
  168:         ///
  169:         /// </summary>
  170:         public static MembershipUser GetUser(object o)
  171:         {
  172:             MembershipUser membershipUser;
  173: 
  174:             membershipUser = System.Web.Security.Membership.GetUser(o);
  175: 
  176:             return membershipUser;
  177:         }
  178:          * /
  179: 
  180:         /*
  181:         ////////////////////////////////////////////////////////////////////////////
  182: 
  183:         /// <summary>
  184:         ///
  185:         /// </summary>
  186:         public static bool UpdateUser(string userName, string oldPassword, string newPassword, string email, DbContext context, out string result)
  187:         {
  188:             bool b;
  189:             MembershipUser membershipUser;
  190: 
  191:             b = false;
  192:             result = "";
  193:             userList = null; // to force refresh in UserList
  194: 
  195:             membershipUser = System.Web.Security.Membership.GetUser(userName);
  196: 
  197:             if (oldPassword.Length > 0 && newPassword.Length > 0)
  198:             {
  199:                 b = membershipUser.ChangePassword(oldPassword, newPassword);
  200:             }
  201: 
  202:             membershipUser.Email = email;
  203: 
  204:             System.Web.Security.Membership.UpdateUser(membershipUser);
  205: 
  206:             b = true;
  207: 
  208:             return b;
  209:         }
  210:          * /
  211: 
  212:         ////////////////////////////////////////////////////////////////////////////
  213: 
  214:         /// <summary>
  215:         ///
  216:         /// </summary>
  217:         public static void DeleteUser(string userName, Microsoft.EntityFrameworkCore.DbContext context, out IdentityResult identityResult)
  218:         {
  219:             IdentityUser identityUser;
  220:             UserStore<IdentityUser> userStore;
  221:             UserManager<IdentityUser> userManager;
  222: 
  223:             userStore = null;// new UserStore<IdentityUser>(context);
  224:             userManager = new UserManager<IdentityUser>(userStore);
  225: 
  226:             identityUser = userManager.FindByName(userName);
  227: 
  228:             identityResult = userManager.Delete(identityUser);
  229:         }
  230: 
  231:         /*
  232:         ////////////////////////////////////////////////////////////////////////////
  233: 
  234:         /// <summary>
  235:         ///
  236:         /// </summary>
  237:         public static bool ValidateUser(string userName, string password, out string result)
  238:         {
  239:             bool b;
  240: 
  241:             result = "";
  242:             b = System.Web.Security.Membership.ValidateUser(userName, password);
  243: 
  244:             if (b)
  245:             {
  246:             }
  247:             else
  248:             {
  249:                 result = "Error: User credentials invalid. ";
  250:             }
  251: 
  252:             return b;
  253:         }
  254:          * /
  255: 
  256:         ////////////////////////////////////////////////////////////////////////////
  257: 
  258:         /// <summary>
  259:         /// Translate MembershipCreateStatus enum value into text
  260:         /// </summary>
  261:         public static string MembershipCreateStatusText(MembershipCreateStatus membershipCreateStatus)
  262:         {
  263:             string s;
  264: 
  265:             switch (membershipCreateStatus)
  266:             {
  267:                 case MembershipCreateStatus.DuplicateEmail: s = "The e-mail address already exists in the database for the application. "; break;
  268:                 case MembershipCreateStatus.DuplicateProviderUserKey: s = "The provider user key already exists in the database for the application. "; break;
  269:                 case MembershipCreateStatus.DuplicateUserName: s = "The user name already exists in the database for the application. "; break;
  270:                 case MembershipCreateStatus.InvalidAnswer: s = "The password retrieval answer provided is invalid. "; break;
  271:                 case MembershipCreateStatus.InvalidEmail: s = "The e-mail address is not formatted correctly. "; break;
  272:                 case MembershipCreateStatus.InvalidPassword: s = "The password is not formatted correctly. "; break;
  273:                 case MembershipCreateStatus.InvalidProviderUserKey: s = "The provider user key is of an invalid type or format. "; break;
  274:                 case MembershipCreateStatus.InvalidQuestion: s = "The password retrieval question provided is invalid. "; break;
  275:                 case MembershipCreateStatus.InvalidUserName: s = "The user name provided is invalid. "; break;
  276:                 case MembershipCreateStatus.ProviderError: s = "The authentication provider returned an error. "; break;
  277:                 case MembershipCreateStatus.Success: s = "The user was successfully created. "; break;
  278:                 case MembershipCreateStatus.UserRejected: s = "The user creation request has been canceled. "; break;
  279:                 default: s = ""; break;
  280:             }
  281: 
  282:             /*
  283: public string GetErrorMessage(MembershipCreateStatus status)
  284: {
  285:    switch (status)
  286:    {
  287:       case MembershipCreateStatus.DuplicateUserName:
  288:         return "Username already exists. Please enter a different user name.";
  289: 
  290:       case MembershipCreateStatus.DuplicateEmail:
  291:         return "A username for that e-mail address already exists. Please enter a different e-mail address.";
  292: 
  293:       case MembershipCreateStatus.InvalidPassword:
  294:         return "The password provided is invalid. Please enter a valid password value.";
  295: 
  296:       case MembershipCreateStatus.InvalidEmail:
  297:         return "The e-mail address provided is invalid. Please check the value and try again.";
  298: 
  299:       case MembershipCreateStatus.InvalidAnswer:
  300:         return "The password retrieval answer provided is invalid. Please check the value and try again.";
  301: 
  302:       case MembershipCreateStatus.InvalidQuestion:
  303:         return "The password retrieval question provided is invalid. Please check the value and try again.";
  304: 
  305:       case MembershipCreateStatus.InvalidUserName:
  306:         return "The user name provided is invalid. Please check the value and try again.";
  307: 
  308:       case MembershipCreateStatus.ProviderError:
  309:         return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  310: 
  311:       case MembershipCreateStatus.UserRejected:
  312:         return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  313: 
  314:       default:
  315:         return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  316:    }
  317: }             * /
  318: 
  319:             return s;
  320:         }
  321: 
  322:         ////////////////////////////////////////////////////////////////////////////
  323: 
  324:         /// <summary>
  325:         ///
  326:         /// </summary>
  327:         public static List<IdentityRole> RoleList(Microsoft.EntityFrameworkCore.DbContext context)
  328:         {
  329:             RoleStore<IdentityRole> roleStore;
  330:             RoleManager<IdentityRole> roleManager;
  331:             List<IdentityRole> identityRoleList;
  332: 
  333:             roleStore = null;// new RoleStore<IdentityRole>(context);
  334:             roleManager = new RoleManager<IdentityRole>(roleStore);
  335: 
  336:             identityRoleList = roleManager.Roles.ToList();
  337: 
  338:             return identityRoleList.ToList();
  339:         }
  340: 
  341:         ////////////////////////////////////////////////////////////////////////////
  342: 
  343:         /// <summary>
  344:         ///
  345:         /// </summary>
  346:         public static void RemoveUserFromRole(string userName, string roleName)
  347:         {
  348:             Roles.RemoveUserFromRole(userName, roleName);
  349:         }
  350: 
  351:         ////////////////////////////////////////////////////////////////////////////
  352: 
  353:         /// <summary>
  354:         ///
  355:         /// </summary>
  356:         public static void CreateRole(string roleName)
  357:         {
  358:             Roles.CreateRole(roleName);
  359:         }
  360: 
  361:         ////////////////////////////////////////////////////////////////////////////
  362: 
  363:         /// <summary>
  364:         ///
  365:         /// </summary>
  366:         public static bool DeleteRole(string roleName)
  367:         {
  368:             return Roles.DeleteRole(roleName);
  369:         }
  370: 
  371:         ////////////////////////////////////////////////////////////////////////////
  372: 
  373:         /// <summary>
  374:         ///
  375:         /// </summary>
  376:         public static void AddUserToRole(string userName, string roleName)
  377:         {
  378:             Roles.AddUserToRole(userName, roleName);
  379:         }
  380: 
  381:         ////////////////////////////////////////////////////////////////////////////
  382: 
  383:         /// <summary>
  384:         ///
  385:         /// </summary>
  386:         public static void AddUserToRoles(string userName, string[] roleNames)
  387:         {
  388:             Roles.AddUserToRoles(userName, roleNames);
  389:         }
  390: 
  391:         ////////////////////////////////////////////////////////////////////////////
  392: 
  393:         /// <summary>
  394:         ///
  395:         /// </summary>
  396:         public static string[] UsersInRole(string roleName)
  397:         {
  398:             return Roles.GetUsersInRole(roleName);
  399:         }
  400: 
  401:         ////////////////////////////////////////////////////////////////////////////
  402: 
  403:         /// <summary>
  404:         ///
  405:         /// </summary>
  406:         public static string[] RolesForUser(string userName)
  407:         {
  408:             return Roles.GetRolesForUser(userName);
  409:         }
  410: 
  411:         ////////////////////////////////////////////////////////////////////////////
  412: 
  413:         /// <summary>
  414:         ///
  415:         /// </summary>
  416:         private static string ExtractFirstNameAndChangeToLowerCase(string name)
  417:         {
  418:             int indexOfFirstSpace;
  419:             string s;
  420: 
  421:             s = null;
  422: 
  423:             if (name != null && name != string.Empty)
  424:             {
  425:                 name = name.Trim().ToLower();
  426: 
  427:                 indexOfFirstSpace = name.IndexOf(' ');
  428: 
  429:                 if (indexOfFirstSpace < 0) indexOfFirstSpace = name.Length - 1;
  430: 
  431:                 s = name.Substring(0, indexOfFirstSpace + 1);
  432:             }
  433: 
  434:             return s;
  435:         }
  436: 
  437:         ////////////////////////////////////////////////////////////////////////////
  438:         ////////////////////////////////////////////////////////////////////////////
  439:     }
  440: */
  441:     ////////////////////////////////////////////////////////////////////////////
  442:     ////////////////////////////////////////////////////////////////////////////
  443:  
  444:  
  445:  
  446:  
  447:     ////////////////////////////////////////////////////////////////////////////
  448:  
  449:     /// <summary>
  450:     ///
  451:     /// </summary>
  452:     public class Identity
  453:     {
  454:         private const int initialUserListLength = 1000;
  455:         private static Dictionary<Guid, string> userNameDictionary;
  456:         private static List<User> userList;
  457:         private static DateTime userListTimestamp;
  458:  
  459:         private static readonly object objectLock = new object();
  460:  
  461:         ////////////////////////////////////////////////////////////////////////////
  462:  
  463:         /// <summary>
  464:         ///
  465:         /// </summary>
  466:         public class User
  467:         {
  468:             /// <summary/>
  469:             public User() { }
  470:  
  471:             /// <summary/>
  472:             public Guid ProviderUserKey { get; set; }
  473:  
  474:             /// <summary/>
  475:             public string UserName { get; set; }
  476:  
  477:             /// <summary/>
  478:             public string Email { get; set; }
  479:         }
  480:  
  481:         ////////////////////////////////////////////////////////////////////////////
  482:  
  483:         /// <summary>
  484:         ///
  485:         /// </summary>
  486:         public static Dictionary<Guid, string> UserNameDictionary
  487:         {
  488:             get
  489:             {
  490:                 lock (objectLock)
  491:                 {
  492:                     if (userNameDictionary == null || userNameDictionary.Count == 0) userNameDictionary = Ia.Cl.Model.Identity._UserNameDictionary;
  493:  
  494:                     return userNameDictionary;
  495:                 }
  496:             }
  497:         }
  498:  
  499:         ////////////////////////////////////////////////////////////////////////////
  500:  
  501:         /// <summary>
  502:         ///
  503:         /// </summary>
  504:         private static Dictionary<Guid, string> _UserNameDictionary
  505:         {
  506:             get
  507:             {
  508:                 MembershipUserCollection memberList;
  509:  
  510:                 memberList = GetAllUsers();
  511:  
  512:                 userNameDictionary = new Dictionary<Guid, string>(memberList.Count);
  513:                 userNameDictionary.Clear();
  514:  
  515:                 userNameDictionary.Add(Guid.Empty, "");
  516:  
  517:                 foreach (MembershipUser mu in memberList)
  518:                 {
  519:                     userNameDictionary.Add(Guid.Parse(mu.ProviderUserKey.ToString()), mu.UserName);
  520:                 }
  521:  
  522:                 return userNameDictionary;
  523:             }
  524:         }
  525:  
  526:         ////////////////////////////////////////////////////////////////////////////
  527:  
  528:         /// <summary>
  529:         ///
  530:         /// </summary>
  531:         public static List<User> UserList
  532:         {
  533:             get
  534:             {
  535:                 lock (objectLock)
  536:                 {
  537:                     if (userList == null || userList.Count == 0) userList = Ia.Cl.Model.Identity._UserList;
  538:  
  539:                     return userList;
  540:                 }
  541:             }
  542:         }
  543:  
  544:         ////////////////////////////////////////////////////////////////////////////
  545:  
  546:         /// <summary>
  547:         ///
  548:         /// </summary>
  549:         private static List<User> _UserList
  550:         {
  551:             get
  552:             {
  553:                 User user;
  554:                 MembershipUserCollection membershipUserCollection;
  555:                 userList = new List<User>(initialUserListLength); // this is to prevent storage errors
  556:  
  557:                 membershipUserCollection = Membership.GetAllUsers();
  558:  
  559:                 foreach (MembershipUser mu in membershipUserCollection)
  560:                 {
  561:                     user = new User
  562:                     {
  563:                         ProviderUserKey = Guid.Parse(mu.ProviderUserKey.ToString()),
  564:                         UserName = mu.UserName,
  565:                         Email = (!mu.Email.Contains("kuix.com")) ? mu.Email : null
  566:                     };
  567:  
  568:                     userList.Add(user);
  569:                 }
  570:  
  571:                 userListTimestamp = DateTime.UtcNow.AddHours(3);
  572:  
  573:                 return userList;
  574:             }
  575:         }
  576:  
  577:         ////////////////////////////////////////////////////////////////////////////
  578:  
  579:         /// <summary>
  580:         ///
  581:         /// </summary>
  582:         public static DateTime UserListTimestamp
  583:         {
  584:             get
  585:             {
  586:                 return userListTimestamp;
  587:             }
  588:         }
  589:  
  590:         ////////////////////////////////////////////////////////////////////////////
  591:  
  592:         /// <summary>
  593:         ///
  594:         /// </summary>
  595:         public static MembershipUserCollection GetAllUsers()
  596:         {
  597:             MembershipUserCollection memberList;
  598:  
  599:             memberList = Membership.GetAllUsers();
  600:  
  601:             return memberList;
  602:         }
  603:  
  604:         ////////////////////////////////////////////////////////////////////////////
  605:  
  606:         /// <summary>
  607:         ///
  608:         /// </summary>
  609:         public static MembershipUser GetUser(object o)
  610:         {
  611:             MembershipUser membershipUser;
  612:  
  613:             membershipUser = System.Web.Security.Membership.GetUser(o);
  614:  
  615:             return membershipUser;
  616:         }
  617:  
  618:         ////////////////////////////////////////////////////////////////////////////
  619:  
  620:         /// <summary>
  621:         ///
  622:         /// </summary>
  623:         public static MembershipUser CreateUser(string userName, string password, string email, out MembershipCreateStatus membershipCreateStatus)
  624:         {
  625:             return CreateUser(userName, password, email, "Q", "A", out membershipCreateStatus);
  626:         }
  627:  
  628:         ////////////////////////////////////////////////////////////////////////////
  629:  
  630:         /// <summary>
  631:         ///
  632:         /// </summary>
  633:         public static MembershipUser CreateUser(string userName, string password, string email, string question, string answer, out MembershipCreateStatus membershipCreateStatus)
  634:         {
  635:             MembershipUser membershipUser;
  636:  
  637:             membershipUser = null;
  638:             userList = null; // to force refresh in UserList
  639:  
  640:             //try
  641:             //{
  642:             membershipUser = System.Web.Security.Membership.CreateUser(userName, password, email, question, answer, true, out membershipCreateStatus);
  643:  
  644:             /*
  645:             // If user created successfully, set password question and answer (if applicable) and 
  646:             // redirect to login page. Otherwise return an error message.
  647: 
  648:             if (Membership.RequiresQuestionAndAnswer)
  649:             {
  650:                 newUser.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text,
  651:                                                         PasswordQuestionTextbox.Text,
  652:                                                         PasswordAnswerTextbox.Text);
  653:             }
  654: 
  655:             Response.Redirect("login.aspx");
  656:              */
  657:             //}
  658:             //catch (MembershipCreateUserException e)
  659:             //{
  660:             //Msg.Text = GetErrorMessage(e.StatusCode);
  661:             //}
  662:             //catch (HttpException e)
  663:             //{
  664:             //Msg.Text = e.Message;
  665:             //}
  666:  
  667:             return membershipUser;
  668:         }
  669:  
  670:         ////////////////////////////////////////////////////////////////////////////
  671:  
  672:         /// <summary>
  673:         ///
  674:         /// </summary>
  675:         public static void UpdateUser(MembershipUser membershipUser)
  676:         {
  677:             System.Web.Security.Membership.UpdateUser(membershipUser);
  678:         }
  679:  
  680:         ////////////////////////////////////////////////////////////////////////////
  681:  
  682:         /// <summary>
  683:         ///
  684:         /// </summary>
  685:         public static bool UpdateUser(string userName, string oldPassword, string newPassword, string email, out Ia.Cl.Model.Result result)
  686:         {
  687:             bool b;
  688:             MembershipUser membershipUser;
  689:  
  690:             b = false;
  691:             result = new Ia.Cl.Model.Result();
  692:  
  693:             userList = null; // to force refresh in UserList
  694:  
  695:             membershipUser = System.Web.Security.Membership.GetUser(userName);
  696:  
  697:             if (oldPassword.Length > 0 && newPassword.Length > 0)
  698:             {
  699:                 b = membershipUser.ChangePassword(oldPassword, newPassword);
  700:  
  701:                 if (b)
  702:                 {
  703:                     result.AddSuccess(@"User """ + userName + @""" password updated.");
  704:                 }
  705:                 else
  706:                 {
  707:                     result.AddError(@"User """ + userName + @""" password not updated.");
  708:                 }
  709:             }
  710:             else
  711:             {
  712:                 result.AddError(@"Old and/or new password length is zero.");
  713:             }
  714:  
  715:             membershipUser.Email = email;
  716:  
  717:             System.Web.Security.Membership.UpdateUser(membershipUser);
  718:  
  719:             b = true;
  720:  
  721:             return b;
  722:         }
  723:  
  724:         ////////////////////////////////////////////////////////////////////////////
  725:  
  726:         /// <summary>
  727:         ///
  728:         /// </summary>
  729:         public static bool DeleteUser(string userName, out Ia.Cl.Model.Result result)
  730:         {
  731:             bool b;
  732:  
  733:             result = new Ia.Cl.Model.Result();
  734:  
  735:             userList = null; // to force refresh in UserList
  736:  
  737:             b = System.Web.Security.Membership.DeleteUser(userName, true);
  738:  
  739:             if (b)
  740:             {
  741:                 result.AddSuccess(@"User """ + userName + @""" deleted.");
  742:             }
  743:             else
  744:             {
  745:                 result.AddError(@"Could not delete user """ + userName + @""".");
  746:             }
  747:  
  748:             return b;
  749:         }
  750:  
  751:         ////////////////////////////////////////////////////////////////////////////
  752:  
  753:         /// <summary>
  754:         ///
  755:         /// </summary>
  756:         public static bool ValidateUser(string userName, string password, out Ia.Cl.Model.Result result)
  757:         {
  758:             bool b;
  759:  
  760:             result = new Ia.Cl.Model.Result();
  761:  
  762:             b = System.Web.Security.Membership.ValidateUser(userName, password);
  763:  
  764:             if (b)
  765:             {
  766:                 result.AddSuccess(@"User """ + userName + @""" credentials validated.");
  767:             }
  768:             else
  769:             {
  770:                 result.AddError(@"User """ + userName + @""" credentials invalid.");
  771:             }
  772:  
  773:             return b;
  774:         }
  775:  
  776:         ////////////////////////////////////////////////////////////////////////////
  777:  
  778:         /// <summary>
  779:         /// Translate MembershipCreateStatus enum value into text
  780:         /// </summary>
  781:         public static string MembershipCreateStatusText(MembershipCreateStatus membershipCreateStatus)
  782:         {
  783:             string s;
  784:  
  785:             switch (membershipCreateStatus)
  786:             {
  787:                 case MembershipCreateStatus.DuplicateEmail: s = "The e-mail address already exists in the database for the application. "; break;
  788:                 case MembershipCreateStatus.DuplicateProviderUserKey: s = "The provider user key already exists in the database for the application. "; break;
  789:                 case MembershipCreateStatus.DuplicateUserName: s = "The user name already exists in the database for the application. "; break;
  790:                 case MembershipCreateStatus.InvalidAnswer: s = "The password retrieval answer provided is invalid. "; break;
  791:                 case MembershipCreateStatus.InvalidEmail: s = "The e-mail address is not formatted correctly. "; break;
  792:                 case MembershipCreateStatus.InvalidPassword: s = "The password is not formatted correctly. "; break;
  793:                 case MembershipCreateStatus.InvalidProviderUserKey: s = "The provider user key is of an invalid type or format. "; break;
  794:                 case MembershipCreateStatus.InvalidQuestion: s = "The password retrieval question provided is invalid. "; break;
  795:                 case MembershipCreateStatus.InvalidUserName: s = "The user name provided is invalid. "; break;
  796:                 case MembershipCreateStatus.ProviderError: s = "The authentication provider returned an error. "; break;
  797:                 case MembershipCreateStatus.Success: s = "The user was successfully created. "; break;
  798:                 case MembershipCreateStatus.UserRejected: s = "The user creation request has been canceled. "; break;
  799:                 default: s = ""; break;
  800:             }
  801:  
  802:             /*
  803: public string GetErrorMessage(MembershipCreateStatus status)
  804: {
  805:    switch (status)
  806:    {
  807:       case MembershipCreateStatus.DuplicateUserName:
  808:         return "Username already exists. Please enter a different user name.";
  809: 
  810:       case MembershipCreateStatus.DuplicateEmail:
  811:         return "A username for that e-mail address already exists. Please enter a different e-mail address.";
  812: 
  813:       case MembershipCreateStatus.InvalidPassword:
  814:         return "The password provided is invalid. Please enter a valid password value.";
  815: 
  816:       case MembershipCreateStatus.InvalidEmail:
  817:         return "The e-mail address provided is invalid. Please check the value and try again.";
  818: 
  819:       case MembershipCreateStatus.InvalidAnswer:
  820:         return "The password retrieval answer provided is invalid. Please check the value and try again.";
  821: 
  822:       case MembershipCreateStatus.InvalidQuestion:
  823:         return "The password retrieval question provided is invalid. Please check the value and try again.";
  824: 
  825:       case MembershipCreateStatus.InvalidUserName:
  826:         return "The user name provided is invalid. Please check the value and try again.";
  827: 
  828:       case MembershipCreateStatus.ProviderError:
  829:         return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  830: 
  831:       case MembershipCreateStatus.UserRejected:
  832:         return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  833: 
  834:       default:
  835:         return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  836:    }
  837: }             */
  838:  
  839:             return s;
  840:         }
  841:  
  842:         ////////////////////////////////////////////////////////////////////////////
  843:  
  844:         /// <summary>
  845:         ///
  846:         /// </summary>
  847:         public static void RemoveUserFromRole(string userName, string roleName)
  848:         {
  849:             Roles.RemoveUserFromRole(userName, roleName);
  850:         }
  851:  
  852:         ////////////////////////////////////////////////////////////////////////////
  853:  
  854:         /// <summary>
  855:         ///
  856:         /// </summary>
  857:         public static string[] GetAllRoles()
  858:         {
  859:             return Roles.GetAllRoles();
  860:         }
  861:  
  862:         ////////////////////////////////////////////////////////////////////////////
  863:  
  864:         /// <summary>
  865:         ///
  866:         /// </summary>
  867:         public static void CreateRole(string roleName)
  868:         {
  869:             Roles.CreateRole(roleName);
  870:         }
  871:  
  872:         ////////////////////////////////////////////////////////////////////////////
  873:  
  874:         /// <summary>
  875:         ///
  876:         /// </summary>
  877:         public static bool DeleteRole(string roleName)
  878:         {
  879:             return Roles.DeleteRole(roleName);
  880:         }
  881:  
  882:         ////////////////////////////////////////////////////////////////////////////
  883:  
  884:         /// <summary>
  885:         ///
  886:         /// </summary>
  887:         public static void AddUserToRole(string userName, string roleName)
  888:         {
  889:             Roles.AddUserToRole(userName, roleName);
  890:         }
  891:  
  892:         ////////////////////////////////////////////////////////////////////////////
  893:  
  894:         /// <summary>
  895:         ///
  896:         /// </summary>
  897:         public static void AddUserToRoles(string userName, string[] roleNames)
  898:         {
  899:             Roles.AddUserToRoles(userName, roleNames);
  900:         }
  901:  
  902:         ////////////////////////////////////////////////////////////////////////////
  903:  
  904:         /// <summary>
  905:         ///
  906:         /// </summary>
  907:         public static string[] UsersInRole(string roleName)
  908:         {
  909:             return Roles.GetUsersInRole(roleName);
  910:         }
  911:  
  912:         ////////////////////////////////////////////////////////////////////////////
  913:  
  914:         /// <summary>
  915:         ///
  916:         /// </summary>
  917:         public static string[] RolesForUser(string userName)
  918:         {
  919:             return Roles.GetRolesForUser(userName);
  920:         }
  921:  
  922:         ////////////////////////////////////////////////////////////////////////////
  923:  
  924:         /// <summary>
  925:         ///
  926:         /// </summary>
  927:         private static string ExtractFirstNameAndChangeToLowerCase(string name)
  928:         {
  929:             int indexOfFirstSpace;
  930:             string s;
  931:  
  932:             s = null;
  933:  
  934:             if (name != null && name != string.Empty)
  935:             {
  936:                 name = name.Trim().ToLower();
  937:  
  938:                 indexOfFirstSpace = name.IndexOf(' ');
  939:  
  940:                 if (indexOfFirstSpace < 0) indexOfFirstSpace = name.Length - 1;
  941:  
  942:                 s = name.Substring(0, indexOfFirstSpace + 1);
  943:             }
  944:  
  945:             return s;
  946:         }
  947:  
  948:         ////////////////////////////////////////////////////////////////////////////
  949:  
  950:         /// <summary>
  951:         ///
  952:         /// </summary>
  953:         public static List<User> InactiveSinceDateTimeUserList(DateTime dateTime)
  954:         {
  955:             User user;
  956:             MembershipUserCollection membershipUserCollection;
  957:             userList = new List<User>(initialUserListLength); // this is to prevent storage errors
  958:  
  959:             membershipUserCollection = Membership.GetAllUsers();
  960:  
  961:             foreach (MembershipUser mu in membershipUserCollection)
  962:             {
  963:                 if (mu.LastActivityDate < dateTime)
  964:                 {
  965:                     user = new User
  966:                     {
  967:                         ProviderUserKey = Guid.Parse(mu.ProviderUserKey.ToString()),
  968:                         UserName = mu.UserName,
  969:                         Email = (!mu.Email.Contains("kuix.com")) ? mu.Email : null
  970:                     };
  971:  
  972:                     userList.Add(user);
  973:                 }
  974:             }
  975:  
  976:             return userList.ToList();
  977:         }
  978:  
  979:         ////////////////////////////////////////////////////////////////////////////
  980:         ////////////////////////////////////////////////////////////////////////////
  981:     }
  982:  
  983:     ////////////////////////////////////////////////////////////////////////////
  984:     ////////////////////////////////////////////////////////////////////////////
  985: }