شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

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 Microsoft.AspNet.Identity.EntityFramework;
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.Data.Entity;
   7:  using System.Linq;
   8:  using System.Web;
   9:  using System.Web.Security;
  10:   
  11:  namespace Ia.Cl.Model
  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-2015 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, 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, 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, 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 = 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, DbContext context)
 107:          {
 108:              UserStore<IdentityUser> userStore;
 109:              IdentityUser identityUser;
 110:   
 111:              userStore = 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(DbContext context)
 124:          {
 125:              UserStore<IdentityUser> userStore;
 126:              //UserManager<IdentityUser> userManager;
 127:   
 128:              userStore = 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, DbContext context, out IdentityResult identityResult)
 218:          {
 219:              IdentityUser identityUser;
 220:              UserStore<IdentityUser> userStore;
 221:              UserManager<IdentityUser> userManager;
 222:   
 223:              userStore = 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(DbContext context)
 328:          {
 329:              RoleStore<IdentityRole> roleStore;
 330:              RoleManager<IdentityRole> roleManager;
 331:              List<IdentityRole> identityRoleList;
 332:   
 333:              roleStore = 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 object objectLock = new object();
 458:   
 459:          ////////////////////////////////////////////////////////////////////////////
 460:   
 461:          /// <summary>
 462:          ///
 463:          /// </summary>
 464:          public class User
 465:          {
 466:              /// <summary/>
 467:              public User() { }
 468:   
 469:              /// <summary/>
 470:              public Guid ProviderUserKey { get; set; }
 471:              /// <summary/>
 472:              public string UserName { get; set; }
 473:              /// <summary/>
 474:              public string Email { get; set; }
 475:          }
 476:   
 477:          ////////////////////////////////////////////////////////////////////////////
 478:   
 479:          /// <summary>
 480:          ///
 481:          /// </summary>
 482:          public static Dictionary<Guid, string> UserNameDictionary
 483:          {
 484:              get
 485:              {
 486:                  if (userNameDictionary == null || userNameDictionary.Count == 0)
 487:                  {
 488:                      if (HttpContext.Current != null && HttpContext.Current.Application["userNameDictionary"] != null)
 489:                      {
 490:                          userNameDictionary = HttpContext.Current.Application["userNameDictionary"] as Dictionary<Guid, string>;
 491:                      }
 492:                      else
 493:                      {
 494:                          userNameDictionary = global::Ia.Cl.Model.Identity._UserNameDictionary;
 495:   
 496:                          if (HttpContext.Current != null) HttpContext.Current.Application["userNameDictionary"] = userNameDictionary;
 497:                      }
 498:                  }
 499:   
 500:                  return userNameDictionary;
 501:              }
 502:          }
 503:   
 504:          ////////////////////////////////////////////////////////////////////////////
 505:   
 506:          /// <summary>
 507:          ///
 508:          /// </summary>
 509:          private static Dictionary<Guid, string> _UserNameDictionary
 510:          {
 511:              get
 512:              {
 513:                  MembershipUserCollection memberList;
 514:   
 515:                  if (userNameDictionary == null || userNameDictionary.Count == 0)
 516:                  {
 517:                      memberList = GetAllUsers();
 518:   
 519:                      userNameDictionary = new Dictionary<Guid, string>(memberList.Count);
 520:                      userNameDictionary.Clear();
 521:   
 522:                      userNameDictionary.Add(Guid.Empty, "");
 523:   
 524:                      foreach (MembershipUser mu in memberList)
 525:                      {
 526:                          userNameDictionary.Add(Guid.Parse(mu.ProviderUserKey.ToString()), mu.UserName);
 527:                      }
 528:                  }
 529:   
 530:                  return userNameDictionary;
 531:              }
 532:          }
 533:   
 534:          ////////////////////////////////////////////////////////////////////////////
 535:   
 536:          /// <summary>
 537:          /// Delete current list to force code to refresh user list
 538:          /// </summary>
 539:          public static void RefreshUserList()
 540:          {
 541:              userList = global::Ia.Cl.Model.Identity._UserList;
 542:          }
 543:   
 544:          ////////////////////////////////////////////////////////////////////////////
 545:   
 546:          /// <summary>
 547:          ///
 548:          /// </summary>
 549:          public static List<User> UserList
 550:          {
 551:              get
 552:              {
 553:                  if (userList == null || userList.Count == 0)
 554:                  {
 555:                      if (HttpContext.Current != null && HttpContext.Current.Application["userList"] != null)
 556:                      {
 557:                          userList = HttpContext.Current.Application["userList"] as List<User>;
 558:                      }
 559:                      else
 560:                      {
 561:                          lock (objectLock)
 562:                          {
 563:                              userList = global::Ia.Cl.Model.Identity._UserList;
 564:   
 565:                              if (HttpContext.Current != null) HttpContext.Current.Application["userList"] = userList;
 566:                          }
 567:                      }
 568:                  }
 569:   
 570:                  return userList;
 571:              }
 572:          }
 573:   
 574:          ////////////////////////////////////////////////////////////////////////////
 575:   
 576:          /// <summary>
 577:          ///
 578:          /// </summary>
 579:          private static List<User> _UserList
 580:          {
 581:              get
 582:              {
 583:                  User user;
 584:                  MembershipUserCollection membershipUserCollection;
 585:                  userList = new List<User>(initialUserListLength); // this is to prevent storage errors
 586:   
 587:                  membershipUserCollection = Membership.GetAllUsers();
 588:   
 589:                  foreach (MembershipUser mu in membershipUserCollection)
 590:                  {
 591:                      user = new User();
 592:   
 593:                      user.ProviderUserKey = Guid.Parse(mu.ProviderUserKey.ToString());
 594:                      user.UserName = mu.UserName;
 595:                      user.Email = (!mu.Email.Contains("kuix.com")) ? mu.Email : null;
 596:   
 597:                      userList.Add(user);
 598:                  }
 599:   
 600:                  return userList.ToList();
 601:              }
 602:          }
 603:   
 604:          ////////////////////////////////////////////////////////////////////////////
 605:   
 606:          /// <summary>
 607:          ///
 608:          /// </summary>
 609:          public static MembershipUserCollection GetAllUsers()
 610:          {
 611:              MembershipUserCollection memberList;
 612:   
 613:              memberList = Membership.GetAllUsers();
 614:   
 615:              return memberList;
 616:          }
 617:   
 618:          ////////////////////////////////////////////////////////////////////////////
 619:   
 620:          /// <summary>
 621:          ///
 622:          /// </summary>
 623:          public static MembershipUser GetUser(object o)
 624:          {
 625:              MembershipUser membershipUser;
 626:   
 627:              membershipUser = System.Web.Security.Membership.GetUser(o);
 628:   
 629:              return membershipUser;
 630:          }
 631:   
 632:          ////////////////////////////////////////////////////////////////////////////
 633:   
 634:          /// <summary>
 635:          ///
 636:          /// </summary>
 637:          public static MembershipUser CreateUser(string userName, string password, string email, out MembershipCreateStatus membershipCreateStatus)
 638:          {
 639:              return CreateUser(userName, password, email, "Q", "A", out membershipCreateStatus);
 640:          }
 641:   
 642:          ////////////////////////////////////////////////////////////////////////////
 643:   
 644:          /// <summary>
 645:          ///
 646:          /// </summary>
 647:          public static MembershipUser CreateUser(string userName, string password, string email, string question, string answer, out MembershipCreateStatus membershipCreateStatus)
 648:          {
 649:              MembershipUser membershipUser;
 650:   
 651:              membershipUser = null;
 652:              userList = null; // to force refresh in UserList
 653:   
 654:              //try
 655:              //{
 656:              membershipUser = System.Web.Security.Membership.CreateUser(userName, password, email, question, answer, true, out membershipCreateStatus);
 657:   
 658:              /*
 659:              // If user created successfully, set password question and answer (if applicable) and 
 660:              // redirect to login page. Otherwise return an error message.
 661:  
 662:              if (Membership.RequiresQuestionAndAnswer)
 663:              {
 664:                  newUser.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text,
 665:                                                          PasswordQuestionTextbox.Text,
 666:                                                          PasswordAnswerTextbox.Text);
 667:              }
 668:  
 669:              Response.Redirect("login.aspx");
 670:               */
 671:              //}
 672:              //catch (MembershipCreateUserException e)
 673:              //{
 674:              //Msg.Text = GetErrorMessage(e.StatusCode);
 675:              //}
 676:              //catch (HttpException e)
 677:              //{
 678:              //Msg.Text = e.Message;
 679:              //}
 680:   
 681:              return membershipUser;
 682:          }
 683:   
 684:          ////////////////////////////////////////////////////////////////////////////
 685:   
 686:          /// <summary>
 687:          ///
 688:          /// </summary>
 689:          public static void UpdateUser(MembershipUser membershipUser)
 690:          {
 691:              System.Web.Security.Membership.UpdateUser(membershipUser);
 692:          }
 693:   
 694:          ////////////////////////////////////////////////////////////////////////////
 695:   
 696:          /// <summary>
 697:          ///
 698:          /// </summary>
 699:          public static bool UpdateUser(string userName, string oldPassword, string newPassword, string email, out string result)
 700:          {
 701:              bool b;
 702:              MembershipUser membershipUser;
 703:   
 704:              b = false;
 705:              result = "";
 706:              userList = null; // to force refresh in UserList
 707:   
 708:              membershipUser = System.Web.Security.Membership.GetUser(userName);
 709:   
 710:              if (oldPassword.Length > 0 && newPassword.Length > 0)
 711:              {
 712:                  b = membershipUser.ChangePassword(oldPassword, newPassword);
 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 string result)
 730:          {
 731:              bool b;
 732:   
 733:              result = "";
 734:              userList = null; // to force refresh in UserList
 735:   
 736:              b = System.Web.Security.Membership.DeleteUser(userName, true);
 737:   
 738:              if (b)
 739:              {
 740:              }
 741:              else
 742:              {
 743:                  result = "Error: Could not delete user. ";
 744:              }
 745:   
 746:              return b;
 747:          }
 748:   
 749:          ////////////////////////////////////////////////////////////////////////////
 750:   
 751:          /// <summary>
 752:          ///
 753:          /// </summary>
 754:          public static bool ValidateUser(string userName, string password, out string result)
 755:          {
 756:              bool b;
 757:   
 758:              result = "";
 759:              b = System.Web.Security.Membership.ValidateUser(userName, password);
 760:   
 761:              if (b)
 762:              {
 763:              }
 764:              else
 765:              {
 766:                  result = "Error: User credentials invalid. ";
 767:              }
 768:   
 769:              return b;
 770:          }
 771:   
 772:          ////////////////////////////////////////////////////////////////////////////
 773:   
 774:          /// <summary>
 775:          /// Translate MembershipCreateStatus enum value into text
 776:          /// </summary>
 777:          public static string MembershipCreateStatusText(MembershipCreateStatus membershipCreateStatus)
 778:          {
 779:              string s;
 780:   
 781:              switch (membershipCreateStatus)
 782:              {
 783:                  case MembershipCreateStatus.DuplicateEmail: s = "The e-mail address already exists in the database for the application. "; break;
 784:                  case MembershipCreateStatus.DuplicateProviderUserKey: s = "The provider user key already exists in the database for the application. "; break;
 785:                  case MembershipCreateStatus.DuplicateUserName: s = "The user name already exists in the database for the application. "; break;
 786:                  case MembershipCreateStatus.InvalidAnswer: s = "The password retrieval answer provided is invalid. "; break;
 787:                  case MembershipCreateStatus.InvalidEmail: s = "The e-mail address is not formatted correctly. "; break;
 788:                  case MembershipCreateStatus.InvalidPassword: s = "The password is not formatted correctly. "; break;
 789:                  case MembershipCreateStatus.InvalidProviderUserKey: s = "The provider user key is of an invalid type or format. "; break;
 790:                  case MembershipCreateStatus.InvalidQuestion: s = "The password retrieval question provided is invalid. "; break;
 791:                  case MembershipCreateStatus.InvalidUserName: s = "The user name provided is invalid. "; break;
 792:                  case MembershipCreateStatus.ProviderError: s = "The authentication provider returned an error. "; break;
 793:                  case MembershipCreateStatus.Success: s = "The user was successfully created. "; break;
 794:                  case MembershipCreateStatus.UserRejected: s = "The user creation request has been canceled. "; break;
 795:                  default: s = ""; break;
 796:              }
 797:   
 798:              /*
 799:  public string GetErrorMessage(MembershipCreateStatus status)
 800:  {
 801:     switch (status)
 802:     {
 803:        case MembershipCreateStatus.DuplicateUserName:
 804:          return "Username already exists. Please enter a different user name.";
 805:  
 806:        case MembershipCreateStatus.DuplicateEmail:
 807:          return "A username for that e-mail address already exists. Please enter a different e-mail address.";
 808:  
 809:        case MembershipCreateStatus.InvalidPassword:
 810:          return "The password provided is invalid. Please enter a valid password value.";
 811:  
 812:        case MembershipCreateStatus.InvalidEmail:
 813:          return "The e-mail address provided is invalid. Please check the value and try again.";
 814:  
 815:        case MembershipCreateStatus.InvalidAnswer:
 816:          return "The password retrieval answer provided is invalid. Please check the value and try again.";
 817:  
 818:        case MembershipCreateStatus.InvalidQuestion:
 819:          return "The password retrieval question provided is invalid. Please check the value and try again.";
 820:  
 821:        case MembershipCreateStatus.InvalidUserName:
 822:          return "The user name provided is invalid. Please check the value and try again.";
 823:  
 824:        case MembershipCreateStatus.ProviderError:
 825:          return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
 826:  
 827:        case MembershipCreateStatus.UserRejected:
 828:          return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
 829:  
 830:        default:
 831:          return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
 832:     }
 833:  }             */
 834:   
 835:              return s;
 836:          }
 837:   
 838:          ////////////////////////////////////////////////////////////////////////////
 839:   
 840:          /// <summary>
 841:          ///
 842:          /// </summary>
 843:          public static void RemoveUserFromRole(string userName, string roleName)
 844:          {
 845:              Roles.RemoveUserFromRole(userName, roleName);
 846:          }
 847:   
 848:          ////////////////////////////////////////////////////////////////////////////
 849:   
 850:          /// <summary>
 851:          ///
 852:          /// </summary>
 853:          public static string[] GetAllRoles()
 854:          {
 855:              return Roles.GetAllRoles();
 856:          }
 857:   
 858:          ////////////////////////////////////////////////////////////////////////////
 859:   
 860:          /// <summary>
 861:          ///
 862:          /// </summary>
 863:          public static void CreateRole(string roleName)
 864:          {
 865:              Roles.CreateRole(roleName);
 866:          }
 867:   
 868:          ////////////////////////////////////////////////////////////////////////////
 869:   
 870:          /// <summary>
 871:          ///
 872:          /// </summary>
 873:          public static bool DeleteRole(string roleName)
 874:          {
 875:              return Roles.DeleteRole(roleName);
 876:          }
 877:   
 878:          ////////////////////////////////////////////////////////////////////////////
 879:   
 880:          /// <summary>
 881:          ///
 882:          /// </summary>
 883:          public static void AddUserToRole(string userName, string roleName)
 884:          {
 885:              Roles.AddUserToRole(userName, roleName);
 886:          }
 887:   
 888:          ////////////////////////////////////////////////////////////////////////////
 889:   
 890:          /// <summary>
 891:          ///
 892:          /// </summary>
 893:          public static void AddUserToRoles(string userName, string[] roleNames)
 894:          {
 895:              Roles.AddUserToRoles(userName, roleNames);
 896:          }
 897:   
 898:          ////////////////////////////////////////////////////////////////////////////
 899:   
 900:          /// <summary>
 901:          ///
 902:          /// </summary>
 903:          public static string[] UsersInRole(string roleName)
 904:          {
 905:              return Roles.GetUsersInRole(roleName);
 906:          }
 907:   
 908:          ////////////////////////////////////////////////////////////////////////////
 909:   
 910:          /// <summary>
 911:          ///
 912:          /// </summary>
 913:          public static string[] RolesForUser(string userName)
 914:          {
 915:              return Roles.GetRolesForUser(userName);
 916:          }
 917:   
 918:          ////////////////////////////////////////////////////////////////////////////
 919:   
 920:          /// <summary>
 921:          ///
 922:          /// </summary>
 923:          private static string ExtractFirstNameAndChangeToLowerCase(string name)
 924:          {
 925:              int indexOfFirstSpace;
 926:              string s;
 927:   
 928:              s = null;
 929:   
 930:              if (name != null && name != string.Empty)
 931:              {
 932:                  name = name.Trim().ToLower();
 933:   
 934:                  indexOfFirstSpace = name.IndexOf(' ');
 935:   
 936:                  if (indexOfFirstSpace < 0) indexOfFirstSpace = name.Length - 1;
 937:   
 938:                  s = name.Substring(0, indexOfFirstSpace + 1);
 939:              }
 940:   
 941:              return s;
 942:          }
 943:   
 944:          ////////////////////////////////////////////////////////////////////////////
 945:   
 946:          /// <summary>
 947:          ///
 948:          /// </summary>
 949:          public static List<User> InactiveSinceDateTimeUserList(DateTime dateTime)
 950:          {
 951:              User user;
 952:              MembershipUserCollection membershipUserCollection;
 953:              userList = new List<User>(initialUserListLength); // this is to prevent storage errors
 954:   
 955:              membershipUserCollection = Membership.GetAllUsers();
 956:   
 957:              foreach (MembershipUser mu in membershipUserCollection)
 958:              {
 959:                  if (mu.LastActivityDate < dateTime)
 960:                  {
 961:                      user = new User();
 962:   
 963:                      user.ProviderUserKey = Guid.Parse(mu.ProviderUserKey.ToString());
 964:                      user.UserName = mu.UserName;
 965:                      user.Email = (!mu.Email.Contains("kuix.com")) ? mu.Email : null;
 966:   
 967:                      userList.Add(user);
 968:                  }
 969:              }
 970:   
 971:              return userList.ToList();
 972:          }
 973:   
 974:          ////////////////////////////////////////////////////////////////////////////
 975:          ////////////////////////////////////////////////////////////////////////////
 976:      }
 977:   
 978:      ////////////////////////////////////////////////////////////////////////////
 979:      ////////////////////////////////////////////////////////////////////////////
 980:  }