1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Data.Common;
7 using System.Configuration;
8 using System.Configuration.Provider;
9 using System.Web.Configuration;
10 using System.Web.Security;
11 using System.Security;
12 using System.Security.Principal;
13 
14 public class Test {
populate_db()15 	static void populate_db ()
16 	{
17 	}
18 
create_user(string username, string email, string password, string pwdQuestion, string pwdAnswer)19 	static void create_user (string username, string email, string password, string pwdQuestion, string pwdAnswer)
20 	{
21 		MembershipCreateStatus status;
22 		Membership.CreateUser (username, password, email, pwdQuestion, pwdAnswer, true, out status);
23 		Console.WriteLine ("create status: {0}", status);
24 	}
25 
generate_password(int length, int numNonAlphNum)26 	static void generate_password (int length, int numNonAlphNum)
27 	{
28 		Console.WriteLine ("generated password = \"{0}\"", Membership.GeneratePassword (length, numNonAlphNum));
29 	}
30 
dump_list(MembershipUserCollection users)31 	static void dump_list (MembershipUserCollection users)
32 	{
33 		Console.WriteLine ("{0} users", users.Count);
34 		foreach (MembershipUser u in users) {
35 			Console.WriteLine ("{0} {1} {2}", u.UserName, u.Email, u.IsLockedOut ? "lockedout" : "notlockedout");
36 		}
37 	}
38 
list_all_users()39 	static void list_all_users ()
40 	{
41 		MembershipUserCollection users = Membership.GetAllUsers();
42 		dump_list (users);
43 	}
44 
validate_user(string username, string password)45 	static void validate_user (string username, string password)
46 	{
47 		if (Membership.ValidateUser (username, password))
48 			Console.WriteLine ("success.");
49 		else
50 			Console.WriteLine ("failure.");
51 	}
52 
unlock_user(string username)53 	static void unlock_user (string username)
54 	{
55 		if (Membership.Provider.UnlockUser (username))
56 			Console.WriteLine ("success.");
57 		else
58 			Console.WriteLine ("failure.");
59 	}
60 
reset_password(string username, string pwdAnswer)61 	static void reset_password (string username, string pwdAnswer)
62 	{
63 		string newPassword = Membership.Provider.ResetPassword (username, pwdAnswer);
64 
65 		if (newPassword == null)
66 			Console.WriteLine ("failure.");
67 		else
68 			Console.WriteLine ("success, new password is \"{0}\"", newPassword);
69 	}
70 
change_password(string username, string oldPwd, string newPwd)71 	static void change_password (string username, string oldPwd, string newPwd)
72 	{
73 		if (Membership.Provider.ChangePassword (username, oldPwd, newPwd))
74 			Console.WriteLine ("success.");
75 		else
76 			Console.WriteLine ("failure.");
77 	}
78 
change_question_answer(string username, string pwd, string question, string answer)79 	static void change_question_answer (string username, string pwd, string question, string answer)
80 	{
81 		if (Membership.Provider.ChangePasswordQuestionAndAnswer (username, pwd, question, answer))
82 			Console.WriteLine ("success.");
83 		else
84 			Console.WriteLine ("failure.");
85 	}
86 
find_user_by_email(string pattern)87 	static void find_user_by_email (string pattern)
88 	{
89 		MembershipUserCollection users = Membership.FindUsersByEmail (pattern);
90 		dump_list (users);
91 	}
92 
find_user_by_name(string pattern)93 	static void find_user_by_name (string pattern)
94 	{
95 		MembershipUserCollection users = Membership.FindUsersByName (pattern);
96 		dump_list (users);
97 	}
98 
get_number_of_users_online()99 	static void get_number_of_users_online ()
100 	{
101 		Console.WriteLine ("Number of online users: {0}", Membership.GetNumberOfUsersOnline ());
102 	}
103 
get_password(string username, string answer)104 	static void get_password (string username, string answer)
105 	{
106 		Console.WriteLine ("password for user {0}: {1}", username, Membership.Provider.GetPassword (username, answer));
107 	}
108 
dump_user(string username)109 	static void dump_user (string username)
110 	{
111 		Console.WriteLine ("info for user: {0}", username);
112 
113 		MembershipUser user = Membership.GetUser (username, false);
114 		Console.WriteLine ("comment: {0}", user.Comment);
115 		Console.WriteLine ("creation date: {0}", user.CreationDate);
116 		Console.WriteLine ("email: {0}", user.Email);
117 		Console.WriteLine ("isApproved: {0}", user.IsApproved);
118 		Console.WriteLine ("isOnline: {0}", user.IsOnline);
119 		Console.WriteLine ("last activity date: {0}", user.LastActivityDate);
120 		Console.WriteLine ("last login date: {0}", user.LastLoginDate);
121 		Console.WriteLine ("last password changed date: {0}", user.LastPasswordChangedDate);
122 		Console.WriteLine ("last lockout date: {0}", user.LastLockoutDate);
123 	}
124 
Usage()125 	static void Usage ()
126 	{
127 		Console.WriteLine ("usage:   just look at test.cs...");
128 	}
129 
Main(string[] args)130 	public static void Main (string[] args) {
131 		if (args.Length == 0) {
132 			Usage ();
133 			return;
134 		}
135 
136 		switch (args[0]) {
137 		case "populate":
138 			populate_db ();
139 			break;
140 		case "createuser":
141 			create_user (args[1], args[2], args[3], args[4], args[5]);
142 			break;
143 		case "listallusers":
144 			list_all_users ();
145 			break;
146 		case "generatepassword":
147 			generate_password (Int32.Parse (args[1]), Int32.Parse (args[2]));
148 			break;
149 		case "validateuser":
150 			validate_user (args[1], args[2]);
151 			break;
152 		case "unlockuser":
153 			unlock_user (args[1]);
154 			break;
155 		case "resetpassword":
156 			reset_password (args[1], args[2]);
157 			break;
158 		case "changepassword":
159 			change_password (args[1], args[2], args[3]);
160 			break;
161 		case "changequestionanswer":
162 			change_question_answer (args[1], args[2], args[3], args[4]);
163 			break;
164 		case "dumpuser":
165 			dump_user (args[1]);
166 			break;
167 		case "findusersbyemail":
168 			find_user_by_email (args[1]);
169 			break;
170 		case "findusersbyname":
171 			find_user_by_name (args[1]);
172 			break;
173 		case "getnumberofusersonline":
174 			get_number_of_users_online ();
175 			break;
176 		case "getpassword":
177 			get_password (args[1], args[2]);
178 			break;
179 		default:
180 			Console.WriteLine ("unknown command {0}", args[0]);
181 			break;
182 		}
183 	}
184 }
185 
186 
187 
188 public class ProvPoker : Toshok.Web.Security.SqlMembershipProvider {
EncryptPassword(byte[] pwd)189 	protected override byte[] EncryptPassword (byte[] pwd) {
190 		Console.WriteLine ("pwd = ({0})", Convert.ToBase64String (pwd));
191 		byte[] buf = base.EncryptPassword (pwd);
192 		Console.WriteLine ("buf = {0} bytes long ({1})", buf.Length, Convert.ToBase64String (buf));
193 		return buf;
194 	}
195 
DecryptPassword(byte[] pwd)196 	protected override byte[] DecryptPassword (byte[] pwd) {
197 		Console.WriteLine ("pwd = ({0})", Convert.ToBase64String (pwd));
198 		byte[] buf = base.DecryptPassword (pwd);
199 		Console.WriteLine ("buf = {0} bytes long ({1})", buf.Length, Convert.ToBase64String (buf));
200 		return buf;
201 	}
202 }
203