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