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