Nannette Thacker ShiningStar.net
<a href="http://weblogs.asp.net/nannettethacker/pages/senior-web-application-developer-consultant.aspx">ASP.net Web Application Development</a>
-
'X' is ambiguous in the namespace
I upgraded my projects with the latest version of a third party control (happened to be Telerik, but I've seen the problem occur other times as well) and got the ominous error on the BUILD:
-
Google Chrome Loses ASP.NET Sessions - Need FavIcon
I had programmed a brilliant web page in ASP.NET 4.0 and lo and behold, this one page lost its sessions in Google Chrome. I could run it locally in debug and could not reproduce the issues in Chrome. Didn't happen in IE or Firefox, only Chrome on the published server.
-
4 Year Anniversary Posting on Weblogs.Asp.Net!
On January 24th, 2012 I celebrate my 4th year of writing on this blog. I have written over 140 posts and have received over 700 comments.
-
ChangePassword Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the ChangePassword method. I welcome any suggestions for improvement.
public override bool ChangePassword(string username, string oldPassword, string newPassword) { try { byte[] hashedNewPassword = HashPassword(newPassword.Trim()); byte[] hashedOldPassword = HashPassword(oldPassword.Trim()); using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserName == username && up.UserPassword == hashedOldPassword); if (u != null) { u.UserPassword = hashedNewPassword; context.SaveChanges(); return true; } else return false; } } catch (InvalidOperationException ex) { throw ex; } catch (ArgumentException) { throw; } }
[SIGNATURE] -
GetUser Methods for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the GetUser methods. I welcome any suggestions for improvement.
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserId == Convert.ToInt32(providerUserKey)); MembershipUser membershipUser = GetMembershipUser(u); return membershipUser; } } public override MembershipUser GetUser(string username, bool userIsOnline) { using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserName == username); MembershipUser membershipUser = GetMembershipUser(u); return membershipUser; } } // custom method to return a UserProfile public UserProfile GetUser(string username) { using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserName == username); return u; } } // custom method to return a UserProfile public UserProfile GetUser(object providerUserKey) { using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserId == Convert.ToInt32(providerUserKey)); return u; } }
[SIGNATURE] -
Custom Error Pages and 404 Page Not Found Error Web.Config Setup
When receiving 404 page not found errors on your web site, and you wish to direct users back to your home page, you need to setup two things in your web.config to handle all instances.
-
CreateUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the CreateUser method. I welcome any suggestions for improvement.
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { // only first 3 fields are passed in from the AccountModels.cs try { status = UsernameExists(username); if (status == MembershipCreateStatus.DuplicateUserName) { return null; } status = DuplicateEmail(email); if (status == MembershipCreateStatus.DuplicateEmail) { return null; } byte[] hashedPassword = HashPassword(password.Trim()); GetValues gv = new GetValues(); string ipAddress = gv.getIPAddress(); int userStatus = Convert.ToInt32(SSS.GlobalListValues.Enums.UserStatusCode.Active); using (var context = new SSSEntities()) { UserProfile newUser = new UserProfile() { Email = email, UserPassword = hashedPassword, UserName = username, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DatePasswordLastChanged = DateTime.Now, DateLastLogin = DateTime.Now, UserStatusCode = userStatus, IpAddress = ipAddress, }; // insert the User Role int userRole = Convert.ToInt32(SSS.GlobalListValues.Enums.UserRoleCode.User_Public); // look up the desired user role : // uses a UserRole join table with a many to many relation // between the UserProfile table and the ListValue table ListValue ur = context.ListValues .SingleOrDefault(lv => lv.ListValueId == userRole); newUser.UserProfileUserRoles.Add(ur); context.UserProfiles.AddObject(newUser); context.SaveChanges(); // NKT: after creation, go back and retrieve the auto-generated identity key and // update the userId's for the created and updated userId int userId = newUser.UserId; newUser.CreatedUserId = userId; newUser.UpdatedUserId = userId; context.SaveChanges(); status = MembershipCreateStatus.Success; return GetMembershipUser(newUser); } } catch (ArgumentException) { status = MembershipCreateStatus.ProviderError; return null; } }
[SIGNATURE] -
DeleteUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the DeleteUser method. I welcome any suggestions for improvement.
public override bool DeleteUser(string username, bool deleteAllRelatedData) { // deleteAllRelatedData not implemented try { using (var context = new SSSEntities()) { UserProfile u = context.UserProfiles .SingleOrDefault(up => up.UserName == username); context.UserProfiles.DeleteObject(u); context.SaveChanges(); return true; } } catch { return false; }
[SIGNATURE] -
UsernameExists & DuplicateEmail Helper Methods for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the UsernameExists & DuplicateEmail Helper methods. I welcome any suggestions for improvement.
// helper method public MembershipCreateStatus UsernameExists(string username) { using (var context = new SSSEntities()) { if (context.UserProfiles.Any( u => u.UserName == username)) { return MembershipCreateStatus.DuplicateUserName; } return MembershipCreateStatus.Success; } } // helper method public MembershipCreateStatus DuplicateEmail(string email) { using (var context = new SSSEntities()) { if (context.UserProfiles.Any( u => u.Email == email)) { return MembershipCreateStatus.DuplicateEmail; } return MembershipCreateStatus.Success; } }
[SIGNATURE] -
GetMembershipUser Helper Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the GetMembershipUser Helper method. I welcome any suggestions for improvement. Since we are working with a UserProfile Entity and the Membership Provider overridable methods require we return a MembershipUser type, below we create our MembershipUser type using values from our UserProfile type so we can return the required type in our methods.
// helper method public MembershipUser GetMembershipUser(UserProfile u) { // copy pertinent UserProfile data to the MembershipUser
// data to be returned as a MembershipUser type object userIDObj = u.UserId; MembershipUser membershipUser = new MembershipUser( this.Name, u.UserName, userIDObj, u.Email, string.Empty, string.Empty, true, false, (DateTime)u.DateCreated, (DateTime)u.DateLastLogin, (DateTime)u.DateUpdated, (DateTime)u.DateLastLogin, (DateTime)u.DateLastLogin); return membershipUser; }
[SIGNATURE]