The SimpleMembershipProvider
is specifically built for usage together with Entity Framework and SQL Server. If you want to use a NoSQL store in stead, you should create your own custom MembershipProvider that inherits from the ExtendedMembershipProvider
class and implement the methods that you need for your functionality to work. Quite a few of them can be left to throw an UnimplementedException
without any negative effects on the rest of your application.
Exception with customer membership provider
-
28-06-2022 - |
Question
I work with ASP.NET MVC4 application. I am trying to implement my own custom membership provider, inherited from SimpleMembership provider class.
My goals: 1. Use custom NoSQL data store. 2. Support both Forms and OAuth (Facebook) authentication in a single provider.
I succeeded to implement user self-registration and forms login. Unfortunately, after the forms login, I get an exception when going to /Account/Manage URL (by clicking on user name).
The exception is in the AccountController method:
public ActionResult Manage(ManageMessageId? message)
{
ViewBag.StatusMessage =
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
: message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
: "";
ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
ViewBag.ReturnUrl = Url.Action("Manage");
return View();
}
User.Identity.Name received is equal to the username of the logged-in user.
Yet the call to
WebSecurity.GetUserId(User.Identity.Name)
throws and exception:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=WebMatrix.WebData
StackTrace:
at WebMatrix.WebData.WebSecurity.GetUserId(String userName)
[continue stack]
In the SimpleMembershipProvider, I see a method to return user ID called for OAuth users, but not for form-authenticated users.
What could be wrong with my implementation?
Thanks.
Solution