| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- //
- // System.Web.Security.Membership
- //
- // Authors:
- // Ben Maurer ([email protected])
- // Lluis Sanchez Gual ([email protected])
- //
- // (C) 2003 Ben Maurer
- // (C) 2005 Novell, inc.
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System.Collections;
- using System.Collections.Specialized;
- using System.Text;
- using System.Web.Configuration;
- using System.Configuration;
- using System.Security.Cryptography;
- namespace System.Web.Security
- {
- public static class Membership
- {
- #if TARGET_J2EE
- const string Membership_providers = "Membership.providers";
- static MembershipProviderCollection providers {
- get {
- object o = AppDomain.CurrentDomain.GetData (Membership_providers);
- if (o == null) {
- lock (AppDomain.CurrentDomain) {
- o = AppDomain.CurrentDomain.GetData (Membership_providers);
- if (o == null) {
- MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
- MembershipProviderCollection local_providers = new MembershipProviderCollection ();
- ProvidersHelper.InstantiateProviders (section.Providers, local_providers, typeof (MembershipProvider));
- AppDomain.CurrentDomain.SetData (Membership_providers, local_providers);
- o = local_providers;
- }
- }
- }
- return (MembershipProviderCollection) o;
- }
- }
- static MembershipProvider provider {
- get {
- MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
- return providers [section.DefaultProvider];
- }
- }
- static int onlineTimeWindow {
- get {
- MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
- return (int) section.UserIsOnlineTimeWindow.TotalMinutes;
- }
- }
- #else
- static MembershipProviderCollection providers;
- static MembershipProvider provider;
- static int onlineTimeWindow;
- static Membership ()
- {
- MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
- providers = new MembershipProviderCollection ();
- ProvidersHelper.InstantiateProviders (section.Providers, providers, typeof (MembershipProvider));
- provider = providers[section.DefaultProvider];
- onlineTimeWindow = (int) section.UserIsOnlineTimeWindow.TotalMinutes;
- }
- #endif
- public static MembershipUser CreateUser (string username, string password)
- {
- return CreateUser (username, password, null);
- }
-
- public static MembershipUser CreateUser (string username, string password, string email)
- {
- MembershipCreateStatus status;
- MembershipUser usr = CreateUser (username, password, email, null, null, true, out status);
- if (usr == null)
- throw new MembershipCreateUserException (status);
-
- return usr;
- }
-
- public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, out MembershipCreateStatus status)
- {
- return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, null, out status);
- }
-
- public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
- {
- return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, providerUserKey, out status);
- }
-
- public static bool DeleteUser (string username)
- {
- return Provider.DeleteUser (username, true);
- }
-
- public static bool DeleteUser (string username, bool deleteAllRelatedData)
- {
- return Provider.DeleteUser (username, deleteAllRelatedData);
- }
-
- public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters)
- {
- RandomNumberGenerator rng = RandomNumberGenerator.Create ();
- byte[] pass_bytes = new byte[length];
- int i;
- int num_nonalpha = 0;
- rng.GetBytes (pass_bytes);
-
- for (i = 0; i < length; i ++) {
- /* convert the random bytes to ascii values 33-126 */
- pass_bytes[i] = (byte)(pass_bytes[i] % 93 + 33);
- /* and count the number of
- * non-alphanumeric characters we have
- * as we go */
- if ((pass_bytes[i] >= 33 && pass_bytes[i] <= 47)
- || (pass_bytes[i] >= 58 && pass_bytes[i] <= 64)
- || (pass_bytes[i] >= 91 && pass_bytes[i] <= 96)
- || (pass_bytes[i] >= 123 && pass_bytes[i] <= 126))
- num_nonalpha++;
- /* get rid of any quotes in the
- * password, just in case they cause
- * problems */
- if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
- pass_bytes[i] ++;
- else if (pass_bytes[i] == 96)
- pass_bytes[i] --;
- }
- if (num_nonalpha < numberOfNonAlphanumericCharacters) {
- /* loop over the array, converting the
- * least number of alphanumeric
- * characters to non-alpha */
- for (i = 0; i < length; i ++) {
- if (num_nonalpha == numberOfNonAlphanumericCharacters)
- break;
- if (pass_bytes[i] >= 48 && pass_bytes[i] <= 57) {
- pass_bytes[i] = (byte)(pass_bytes[i] - 48 + 33);
- num_nonalpha++;
- }
- else if (pass_bytes[i] >= 65 && pass_bytes[i] <= 90) {
- pass_bytes[i] = (byte)((pass_bytes[i] - 65) % 13 + 33);
- num_nonalpha++;
- }
- else if (pass_bytes[i] >= 97 && pass_bytes[i] <= 122) {
- pass_bytes[i] = (byte)((pass_bytes[i] - 97) % 13 + 33);
- num_nonalpha++;
- }
- /* and make sure we don't end up with quote characters */
- if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
- pass_bytes[i]++;
- else if (pass_bytes[i] == 96)
- pass_bytes[i] --;
- }
- }
- return Encoding.ASCII.GetString (pass_bytes);
- }
-
- public static MembershipUserCollection GetAllUsers ()
- {
- int total;
- return GetAllUsers (0, int.MaxValue, out total);
- }
-
- public static MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
- {
- return Provider.GetAllUsers (pageIndex, pageSize, out totalRecords);
- }
-
- public static int GetNumberOfUsersOnline ()
- {
- return Provider.GetNumberOfUsersOnline ();
- }
-
- public static MembershipUser GetUser ()
- {
- return GetUser (HttpContext.Current.User.Identity.Name, true);
- }
-
- public static MembershipUser GetUser (bool userIsOnline)
- {
- return GetUser (HttpContext.Current.User.Identity.Name, userIsOnline);
- }
-
- public static MembershipUser GetUser (string username)
- {
- return GetUser (username, false);
- }
-
- public static MembershipUser GetUser (string username, bool userIsOnline)
- {
- return Provider.GetUser (username, userIsOnline);
- }
-
- public static MembershipUser GetUser (object providerUserKey)
- {
- return GetUser (providerUserKey, false);
- }
-
- public static MembershipUser GetUser (object providerUserKey, bool userIsOnline)
- {
- return Provider.GetUser (providerUserKey, userIsOnline);
- }
-
- public static string GetUserNameByEmail (string email)
- {
- return Provider.GetUserNameByEmail (email);
- }
-
- public static void UpdateUser (MembershipUser user)
- {
- Provider.UpdateUser (user);
- }
-
- public static bool ValidateUser (string username, string password)
- {
- return Provider.ValidateUser (username, password);
- }
-
- public static MembershipUserCollection FindUsersByEmail (string emailToMatch)
- {
- int totalRecords;
- return Provider.FindUsersByEmail (emailToMatch, 0, int.MaxValue, out totalRecords);
- }
-
- public static MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
- {
- return Provider.FindUsersByEmail (emailToMatch, pageIndex, pageSize, out totalRecords);
- }
-
- public static MembershipUserCollection FindUsersByName (string nameToMatch)
- {
- int totalRecords;
- return Provider.FindUsersByName (nameToMatch, 0, int.MaxValue, out totalRecords);
- }
-
- public static MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
- {
- return Provider.FindUsersByName (nameToMatch, pageIndex, pageSize, out totalRecords);
- }
- public static string ApplicationName {
- get { return Provider.ApplicationName; }
- set { Provider.ApplicationName = value; }
- }
-
- public static bool EnablePasswordReset {
- get { return Provider.EnablePasswordReset; }
- }
-
- public static bool EnablePasswordRetrieval {
- get { return Provider.EnablePasswordRetrieval; }
- }
-
- public static bool RequiresQuestionAndAnswer {
- get { return Provider.RequiresQuestionAndAnswer; }
- }
-
- public static int MaxInvalidPasswordAttempts {
- get { return Provider.MaxInvalidPasswordAttempts; }
- }
-
- public static int MinRequiredNonAlphanumericCharacters {
- get { return Provider.MinRequiredNonAlphanumericCharacters; }
- }
-
- public static int MinRequiredPasswordLength {
- get { return Provider.MinRequiredPasswordLength; }
- }
-
- public static int PasswordAttemptWindow {
- get { return Provider.PasswordAttemptWindow; }
- }
-
- public static string PasswordStrengthRegularExpression {
- get { return Provider.PasswordStrengthRegularExpression; }
- }
-
- public static MembershipProvider Provider {
- get { return provider; }
- }
-
- public static MembershipProviderCollection Providers {
- get { return providers; }
- }
-
- public static int UserIsOnlineTimeWindow {
- get { return onlineTimeWindow; }
- }
-
- public static event MembershipValidatePasswordEventHandler ValidatingPassword {
- add { Provider.ValidatingPassword += value; }
- remove { Provider.ValidatingPassword -= value; }
- }
- }
- }
- #endif
|