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