)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Identity (Ia.Cl.Models)

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

ASP.NET Identity support class.

    1: using Microsoft.AspNetCore.Identity;
    2: using System;
    3: using System.Collections.Generic;
    4: using System.Linq;
    5:  
    6: namespace Ia.Cl.Models
    7: {
    8:     ////////////////////////////////////////////////////////////////////////////
    9:  
   10:     /// <summary publish="true">
   11:     /// ASP.NET Identity support class.
   12:     /// </summary>
   13:     /// <remarks> 
   14:     /// Copyright © 2014-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   15:     ///
   16:     /// 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
   17:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   18:     ///
   19:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   20:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   21:     /// 
   22:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   23:     /// 
   24:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   25:     /// </remarks> 
   26:     public class Identity
   27:     {
   28:         private static Dictionary<Guid, string> guidToUserNameDictionary;
   29:         private static List<IdentityUser> identityUserList;
   30:         private static List<IdentityRole> identityRoleList;
   31:  
   32:         private static readonly object objectLock = new object();
   33:  
   34:         ////////////////////////////////////////////////////////////////////////////
   35:  
   36:         /// <summary>
   37:         ///
   38:         /// </summary>
   39:         public static IdentityUser CreateUser(UserManager<IdentityUser> userManager, string userName, string password, string confirmPassword, out IdentityResult identityResult)
   40:         {
   41:             return CreateUser(userManager, userName, password, confirmPassword, null, null, null, out identityResult);
   42:         }
   43:  
   44:         ////////////////////////////////////////////////////////////////////////////
   45:  
   46:         /// <summary>
   47:         ///
   48:         /// </summary>
   49:         public static IdentityUser CreateUser(UserManager<IdentityUser> userManager, string userName, string password, string confirmPassword, string email, out IdentityResult identityResult)
   50:         {
   51:             return CreateUser(userManager, userName, password, confirmPassword, email, null, null, out identityResult);
   52:         }
   53:  
   54:         ////////////////////////////////////////////////////////////////////////////
   55:  
   56:         /// <summary>
   57:         ///
   58:         /// </summary>
   59:         public static IdentityUser CreateUserWithNoEmailNoPassword(UserManager<IdentityUser> userManager, string userName, out IdentityResult identityResult)
   60:         {
   61:             return CreateUser(userManager, userName, null, null, null, null, null, out identityResult);
   62:         }
   63:  
   64:         ////////////////////////////////////////////////////////////////////////////
   65:  
   66:         /// <summary>
   67:         ///
   68:         /// </summary>
   69:         public static IdentityUser CreateUser(UserManager<IdentityUser> userManager, string userName, string password, string confirmPassword, string email, string question, string answer, out IdentityResult identityResult)
   70:         {
   71:             var identityUser = new IdentityUser() { UserName = userName, Email = email };
   72:  
   73:             identityResult = userManager.CreateAsync(identityUser, password).Result;
   74:  
   75:             identityUserList = null;
   76:  
   77:             return identityUser;
   78:         }
   79:  
   80:         ////////////////////////////////////////////////////////////////////////////
   81:  
   82:         /// <summary>
   83:         ///
   84:         /// </summary>
   85:         public static IdentityUser CreateUser(UserManager<IdentityUser> userManager, IdentityUser identityUser, string password, out IdentityResult identityResult)
   86:         {
   87:             identityResult = userManager.CreateAsync(identityUser, password).Result;
   88:  
   89:             identityUserList = null;
   90:  
   91:             return identityUser;
   92:         }
   93:  
   94:         ////////////////////////////////////////////////////////////////////////////
   95:  
   96:         /// <summary>
   97:         ///
   98:         /// </summary>
   99:         public static bool SignIn(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, string userName, string password)
  100:         {
  101:             bool signedIn;
  102:  
  103:             var identityUser = userManager.FindByNameAsync(userName).Result;
  104:  
  105:             if (identityUser != null)
  106:             {
  107:                 signInManager.SignInAsync(identityUser, true);
  108:  
  109:                 signedIn = true;
  110:             }
  111:             else signedIn = false;
  112:  
  113:             return signedIn;
  114:         }
  115:  
  116:         ////////////////////////////////////////////////////////////////////////////
  117:  
  118:         /// <summary>
  119:         ///
  120:         /// </summary>
  121:         public static void SignOut(SignInManager<IdentityUser> signInManager)
  122:         {
  123:             signInManager.SignOutAsync().Wait();
  124:         }
  125:  
  126:         ////////////////////////////////////////////////////////////////////////////
  127:  
  128:         /// <summary>
  129:         ///
  130:         /// </summary>
  131:         public static IdentityUser UserByUserName(UserManager<IdentityUser> userManager, string userName)
  132:         {
  133:             return userManager.FindByNameAsync(userName).Result;
  134:         }
  135:  
  136:         ////////////////////////////////////////////////////////////////////////////
  137:  
  138:         /// <summary>
  139:         /// 
  140:         /// </summary>
  141:         public static List<IdentityUser> IdentityUserList(UserManager<IdentityUser> userManager)
  142:         {
  143:             if (identityUserList == null || identityUserList.Count == 0)
  144:             {
  145:                 lock (objectLock)
  146:                 {
  147:                     identityUserList = userManager.Users.ToList();
  148:                 }
  149:             }
  150:  
  151:             return identityUserList;
  152:         }
  153:  
  154:         ////////////////////////////////////////////////////////////////////////////
  155:  
  156:         /// <summary>
  157:         ///
  158:         /// </summary>
  159:         public static bool ChangePassword(UserManager<IdentityUser> userManager, string userIdentityName, string currentPassword, string newPassword, string confirmNewPassword, out IdentityResult identityResult)
  160:         {
  161:             bool passwordChanged;
  162:  
  163:             var identityUser = userManager.FindByNameAsync(userIdentityName).Result;
  164:  
  165:             if (identityUser != null)
  166:             {
  167:                 identityResult = userManager.ChangePasswordAsync(identityUser, currentPassword, newPassword).Result;
  168:  
  169:                 if (identityResult.Succeeded)
  170:                 {
  171:                     passwordChanged = true;
  172:                 }
  173:                 else passwordChanged = false;
  174:             }
  175:             else
  176:             {
  177:                 identityResult = new IdentityResult();
  178:  
  179:                 passwordChanged = false;
  180:             }
  181:  
  182:             return passwordChanged;
  183:         }
  184:  
  185:         ////////////////////////////////////////////////////////////////////////////
  186:  
  187:         /// <summary>
  188:         ///
  189:         /// </summary>
  190:         public static bool UpdateUser(UserManager<IdentityUser> userManager, string userName, string oldPassword, string newPassword, string newEmail, string changeEmailToken, out Ia.Cl.Models.Result result)
  191:         {
  192:             bool b = false;
  193:             IdentityResult identityResult, identityResult2;
  194:  
  195:             result = new Ia.Cl.Models.Result();
  196:  
  197:             var identityUser = userManager.FindByNameAsync(userName).Result;
  198:  
  199:             if (identityUser != null)
  200:             {
  201:                 if (oldPassword.Length > 0 && newPassword.Length > 0)
  202:                 {
  203:                     identityResult = userManager.ChangePasswordAsync(identityUser, oldPassword, newPassword).Result;
  204:  
  205:                     identityResult2 = userManager.ChangeEmailAsync(identityUser, newEmail, changeEmailToken).Result;
  206:  
  207:                     result.AddSuccess(@"User """ + userName + @""" updated.");
  208:                 }
  209:                 else
  210:                 {
  211:                     result.AddError(@"oldPassword.Length == 0 || newPassword.Length == 0.");
  212:                 }
  213:             }
  214:             else
  215:             {
  216:                 result.AddError(@"identityUser == null.");
  217:             }
  218:  
  219:             identityUserList = null;
  220:  
  221:             return b;
  222:         }
  223:  
  224:         ////////////////////////////////////////////////////////////////////////////
  225:  
  226:         /// <summary>
  227:         ///
  228:         /// </summary>
  229:         public static IdentityResult DeleteUser(UserManager<IdentityUser> userManager, string userName)
  230:         {
  231:             IdentityResult identityResult;
  232:  
  233:             identityResult = null;
  234:  
  235:             var identityUser = userManager.FindByNameAsync(userName).Result;
  236:  
  237:             if (identityUser != null)
  238:             {
  239:                 identityResult = userManager.DeleteAsync(identityUser).Result;
  240:             }
  241:             else identityUser = null;
  242:  
  243:             identityUserList = null;
  244:  
  245:             return identityResult;
  246:         }
  247:  
  248:         /*
  249:         ////////////////////////////////////////////////////////////////////////////
  250: 
  251:         /// <summary>
  252:         ///
  253:         /// </summary>
  254:         public static string MembershipCreateStatusText(MembershipCreateStatus membershipCreateStatus)
  255:         {
  256:             string s;
  257: 
  258:             switch (membershipCreateStatus)
  259:             {
  260:                 case MembershipCreateStatus.DuplicateEmail: s = "The e-mail address already exists in the database for the application. "; break;
  261:                 case MembershipCreateStatus.DuplicateProviderUserKey: s = "The provider user key already exists in the database for the application. "; break;
  262:                 case MembershipCreateStatus.DuplicateUserName: s = "The user name already exists in the database for the application. "; break;
  263:                 case MembershipCreateStatus.InvalidAnswer: s = "The password retrieval answer provided is invalid. "; break;
  264:                 case MembershipCreateStatus.InvalidEmail: s = "The e-mail address is not formatted correctly. "; break;
  265:                 case MembershipCreateStatus.InvalidPassword: s = "The password is not formatted correctly. "; break;
  266:                 case MembershipCreateStatus.InvalidProviderUserKey: s = "The provider user key is of an invalid type or format. "; break;
  267:                 case MembershipCreateStatus.InvalidQuestion: s = "The password retrieval question provided is invalid. "; break;
  268:                 case MembershipCreateStatus.InvalidUserName: s = "The user name provided is invalid. "; break;
  269:                 case MembershipCreateStatus.ProviderError: s = "The authentication provider returned an error. "; break;
  270:                 case MembershipCreateStatus.Success: s = "The user was successfully created. "; break;
  271:                 case MembershipCreateStatus.UserRejected: s = "The user creation request has been canceled. "; break;
  272:                 default: s = ""; break;
  273:             }
  274: 
  275:             ////////////////////////////////////////////////////////////////////////////
  276: 
  277:             /// <summary>
  278:             ///
  279:             /// </summary>
  280:             public static string GetErrorMessage(MembershipCreateStatus status)
  281:             {
  282:                 switch (status)
  283:                 {
  284:                     case MembershipCreateStatus.DuplicateUserName:
  285:                         return "Username already exists. Please enter a different user name.";
  286: 
  287:                     case MembershipCreateStatus.DuplicateEmail:
  288:                         return "A username for that e-mail address already exists. Please enter a different e-mail address.";
  289: 
  290:                     case MembershipCreateStatus.InvalidPassword:
  291:                         return "The password provided is invalid. Please enter a valid password value.";
  292: 
  293:                     case MembershipCreateStatus.InvalidEmail:
  294:                         return "The e-mail address provided is invalid. Please check the value and try again.";
  295: 
  296:                     case MembershipCreateStatus.InvalidAnswer:
  297:                         return "The password retrieval answer provided is invalid. Please check the value and try again.";
  298: 
  299:                     case MembershipCreateStatus.InvalidQuestion:
  300:                         return "The password retrieval question provided is invalid. Please check the value and try again.";
  301: 
  302:                     case MembershipCreateStatus.InvalidUserName:
  303:                         return "The user name provided is invalid. Please check the value and try again.";
  304: 
  305:                     case MembershipCreateStatus.ProviderError:
  306:                         return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  307: 
  308:                     case MembershipCreateStatus.UserRejected:
  309:                         return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  310: 
  311:                     default:
  312:                         return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  313:                 }
  314:             }
  315: 
  316:             return s;
  317:         }
  318:         */
  319:  
  320:         ////////////////////////////////////////////////////////////////////////////
  321:  
  322:         /// <summary>
  323:         /// 
  324:         /// </summary>
  325:         public static List<IdentityRole> IdentityRoleList(RoleManager<IdentityRole> roleManager)
  326:         {
  327:             if (identityRoleList == null || identityRoleList.Count == 0)
  328:             {
  329:                 lock (objectLock)
  330:                 {
  331:                     identityRoleList = roleManager.Roles.ToList();
  332:                 }
  333:             }
  334:  
  335:             return identityRoleList;
  336:         }
  337:  
  338:         ////////////////////////////////////////////////////////////////////////////
  339:  
  340:         /// <summary>
  341:         ///
  342:         /// </summary>
  343:         public static IdentityResult CreateRole(RoleManager<IdentityRole> roleManager, string roleName)
  344:         {
  345:             var identityRole = new IdentityRole() { Name = roleName };
  346:  
  347:             return CreateRole(roleManager, identityRole);
  348:         }
  349:  
  350:         ////////////////////////////////////////////////////////////////////////////
  351:  
  352:         /// <summary>
  353:         ///
  354:         /// </summary>
  355:         public static IdentityResult CreateRole(RoleManager<IdentityRole> roleManager, IdentityRole identityRole)
  356:         {
  357:             var identityResult = roleManager.CreateAsync(identityRole).Result;
  358:  
  359:             identityUserList = null;
  360:             identityRoleList = null;
  361:  
  362:             return identityResult;
  363:         }
  364:  
  365:         ////////////////////////////////////////////////////////////////////////////
  366:  
  367:         /// <summary>
  368:         ///
  369:         /// </summary>
  370:         public static bool RoleExists(RoleManager<IdentityRole> roleManager, string roleName)
  371:         {
  372:             var roleExists = roleManager.RoleExistsAsync(roleName).Result;
  373:  
  374:             return roleExists;
  375:         }
  376:  
  377:         ////////////////////////////////////////////////////////////////////////////
  378:  
  379:         /// <summary>
  380:         ///
  381:         /// </summary>
  382:         public static IdentityResult DeleteRole(RoleManager<IdentityRole> roleManager, string roleName)
  383:         {
  384:             var identityRole = roleManager.FindByNameAsync(roleName).Result;
  385:  
  386:             var identityResult = roleManager.DeleteAsync(identityRole).Result;
  387:  
  388:             identityUserList = null;
  389:             identityRoleList = null;
  390:  
  391:             return identityResult;
  392:         }
  393:  
  394:         ////////////////////////////////////////////////////////////////////////////
  395:  
  396:         /// <summary>
  397:         ///
  398:         /// </summary>
  399:         public static IdentityResult AddUserToRole(UserManager<IdentityUser> userManager, string userName, string roleName)
  400:         {
  401:             IdentityResult identityResult;
  402:  
  403:             var identityUser = userManager.FindByNameAsync(userName).Result;
  404:  
  405:             if (identityUser != null)
  406:             {
  407:                 identityResult = userManager.AddToRoleAsync(identityUser, roleName).Result;
  408:  
  409:                 identityUserList = null;
  410:                 identityRoleList = null;
  411:             }
  412:             else identityResult = null;
  413:  
  414:             return identityResult;
  415:         }
  416:  
  417:         ////////////////////////////////////////////////////////////////////////////
  418:  
  419:         /// <summary>
  420:         ///
  421:         /// </summary>
  422:         public static IdentityResult AddUserToRoleList(UserManager<IdentityUser> userManager, string userName, List<string> roleNameList)
  423:         {
  424:             IdentityResult identityResult;
  425:  
  426:             var identityUser = userManager.FindByNameAsync(userName).Result;
  427:  
  428:             if (identityUser != null)
  429:             {
  430:                 identityResult = userManager.AddToRolesAsync(identityUser, roleNameList).Result;
  431:  
  432:                 identityUserList = null;
  433:                 identityRoleList = null;
  434:             }
  435:             else identityResult = null;
  436:  
  437:             return identityResult;
  438:         }
  439:  
  440:         ////////////////////////////////////////////////////////////////////////////
  441:  
  442:         /// <summary>
  443:         ///
  444:         /// </summary>
  445:         public static IdentityResult RemoveUserFromRole(RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager, string userName, string roleName)
  446:         {
  447:             IdentityResult identityResult;
  448:  
  449:             var identityUser = userManager.FindByNameAsync(userName).Result;
  450:  
  451:             if (identityUser != null)
  452:             {
  453:                 identityResult = userManager.RemoveFromRoleAsync(identityUser, roleName).Result;
  454:  
  455:                 identityUserList = null;
  456:                 identityRoleList = null;
  457:             }
  458:             else identityResult = null;
  459:  
  460:             return identityResult;
  461:         }
  462:  
  463:         ////////////////////////////////////////////////////////////////////////////
  464:  
  465:         /// <summary>
  466:         ///
  467:         /// </summary>
  468:         public static IdentityResult RemoveUserFromRoles(RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager, string userName, List<string> roleNameList)
  469:         {
  470:             IdentityResult identityResult;
  471:  
  472:             var identityUser = userManager.FindByNameAsync(userName).Result;
  473:  
  474:             if (identityUser != null)
  475:             {
  476:                 identityResult = userManager.RemoveFromRolesAsync(identityUser, roleNameList).Result;
  477:  
  478:                 identityUserList = null;
  479:                 identityRoleList = null;
  480:             }
  481:             else identityResult = null;
  482:  
  483:             return identityResult;
  484:         }
  485:  
  486:         ////////////////////////////////////////////////////////////////////////////
  487:  
  488:         /// <summary>
  489:         ///
  490:         /// </summary>
  491:         public static List<IdentityUser> UsersInRoleList(UserManager<IdentityUser> userManager, string roleName)
  492:         {
  493:             return userManager.GetUsersInRoleAsync(roleName).Result.ToList();
  494:         }
  495:  
  496:         ////////////////////////////////////////////////////////////////////////////
  497:  
  498:         /// <summary>
  499:         ///
  500:         /// </summary>
  501:         public static bool IsUserInRole(UserManager<IdentityUser> userManager, string userName, string roleName)
  502:         {
  503:             bool userIsInRole;
  504:  
  505:             var identityUser = userManager.FindByNameAsync(userName).Result;
  506:  
  507:             if (identityUser != null)
  508:             {
  509:                 userIsInRole = userManager.IsInRoleAsync(identityUser, roleName).Result;
  510:             }
  511:             else userIsInRole = false;
  512:  
  513:             return userIsInRole;
  514:         }
  515:  
  516:         ////////////////////////////////////////////////////////////////////////////
  517:  
  518:         /// <summary>
  519:         ///
  520:         /// </summary>
  521:         public static List<string> RolesForUserList(UserManager<IdentityUser> userManager, string userName)
  522:         {
  523:             var roleNameList = new List<string>();
  524:  
  525:             var identityUser = userManager.FindByNameAsync(userName).Result;
  526:  
  527:             if (identityUser != null)
  528:             {
  529:                 roleNameList = userManager.GetRolesAsync(identityUser).Result.ToList();
  530:             }
  531:  
  532:             return roleNameList;
  533:         }
  534:  
  535:         /*
  536:         ////////////////////////////////////////////////////////////////////////////
  537: 
  538:         /// <summary>
  539:         ///
  540:         /// </summary>
  541:         public static List<IdentityUser> InactiveSinceDateTimeUserList(UserManager<IdentityUser> userManager, DateTime dateTime)
  542:         {
  543:             IdentityUser identityUser;
  544: 
  545:             userManager.Users.
  546:             MembershipUserCollection membershipUserCollection;
  547:             userList = new List<IdentityUser>(initialUserListLength); // this is to prevent storage errors
  548: 
  549:             membershipUserCollection = Membership.GetAllUsers();
  550: 
  551:             foreach (IdentityUser mu in membershipUserCollection)
  552:             {
  553:                 if (mu.LastActivityDate < dateTime)
  554:                 {
  555:                     user = new LocalUser
  556:                     {
  557:                         ProviderUserKey = Guid.Parse(mu.ProviderUserKey.ToString()),
  558:                         UserName = mu.UserName,
  559:                         Email = (!mu.Email.Contains("kuix.com")) ? mu.Email : null
  560:                     };
  561: 
  562:                     userList.Add(user);
  563:                 }
  564:             }
  565: 
  566:             return userList.ToList();
  567:         }
  568:         */
  569:  
  570:         ////////////////////////////////////////////////////////////////////////////
  571:         ////////////////////////////////////////////////////////////////////////////
  572:     }
  573:  
  574:     ////////////////////////////////////////////////////////////////////////////
  575:     ////////////////////////////////////////////////////////////////////////////
  576: }