Asked by:
User Manager for Webmatrix Users

Question
-
User-2051275383 posted
Good morning all,
I have come across an issue which I am assuming others have faced as well. I have built my authentication similar to what you would see in the Starter Site; User Registers, Logs in, User can change their password, etc.
However I have a site that doesn't have to an SMTP server, so when a user forget's their username - in the site's current state - I have to pretty much delete the account and re-create it.. There is obviously a huge range of issues that comes with doing that.
Some credit is to go to Mike as I have built the user manager on some of the code within his WebPagesCMS. Whilst the way I have coded it might not be the correct way, it works and happy for others to modify for their needs (or improve it).
I wanted to share this with the community, others may want to add this to their projects or the Starter Site. I have added some comments to explain what the code does, some of it is obvious but it may assist new people that want to implement this.
My code assumes you have an admin role setup, that you put these files in a folder called Admin in the base of your project and your database is called Database
I have created a ManageUsers.cshtml - this is what it looks like:@{ @** Ensure that the user that is doing this is an admin **@ if ( !Roles.IsUserInRole("admin")) { Response.Redirect("~/"); } @** Declare database name and then grab all users **@ var db = Database.Open("Database"); var GrabQuery = "SELECT UserId, Email FROM UserProfile"; var GrabUsers = db.Query(GrabQuery); } <h3>Manage User Accounts</h3> <div> @foreach(var row in GrabUsers){ <li>@row.Email | <a href="@Href("~/Admin/ChangePass/" + row.UserId + "")"> Reset Password</a></li> } </div>
This will return a list of all users from your database with a link to reset their password.
The second page that does the password resets is called ChangePass.cshtml - here is the code:@{ @** Ensure that the user that is doing this is an admin **@ if ( !Roles.IsUserInRole("admin")) { Response.Redirect("~/"); } @** Give me the user otherwise I can't help **@ var UserId = UrlData[0]; if (!UserId.IsInt()) { Response.SetStatus(HttpStatusCode.BadRequest); } @** Open database and look for user... **@ var db = Database.Open("Database"); var GrabQuery = @"SELECT Email FROM UserProfile WHERE UserId = @0"; var GrabUser = db.QuerySingle(GrabQuery, UserId); @** If I can't find the user, then I can't help you **@ if (GrabUser == null) { Response.Redirect("~/Admin/ManageUsers"); } @** New password from from, create reset token and then apply it **@ if (IsPost) { var NewPass = Request["formNewPassword"]; if (!NewPass.IsEmpty()) { var CreateToken = WebSecurity.GeneratePasswordResetToken(GrabUser.Email); WebSecurity.ResetPassword(CreateToken, NewPass); } @** Once complete, take me back to user list.. **@ Response.Redirect("~/Admin/ManageUsers"); } } <form method="post"> <h3>Change User Password For @GrabUser.Email</h3> <div> <label>New Password:</label> <input type="text" name="formNewPassword" /> </div> <div> <input type="submit" value="Confirm Change" /> </div> </form>
Hopefully this will assist others as it has done for me - Once again, big thanks to Mike for all his work to the Web Pages community!Saturday, October 11, 2014 8:30 PM
All replies
-
User-457298706 posted
Thanks for sharing such wonderful information a-rad (and Mike for providing tutorials/information to help people still finding their way in ASP.net) . I was searching the web to see if I could get a starting point on how to implement an admin page that allowed the admin to delete desired users once they've been displayed in a grid/list for my hobby project. This code can give me something to play with and adapt to my needs.
Wednesday, October 15, 2014 2:12 AM -
User-2051275383 posted
Thanks for sharing such wonderful information a-rad (and Mike for providing tutorials/information to help people still finding their way in ASP.net) . I was searching the web to see if I could get a starting point on how to implement an admin page that allowed the admin to delete desired users once they've been displayed in a grid/list for my hobby project. This code can give me something to play with and adapt to my needs.
Not a problem LearningRobot - Happy to help. If you need any assistance, please feel free to post!
I have to create a role manager soon, which will encompass this as well.. I will post it when I get time to build it!
Cheers!Wednesday, October 15, 2014 4:25 AM