Eric Yeoman
Eric Yeoman

Reputation: 1036

MVC 4 converts a Dictionary to RouteValueDictionary

I'm experiencing problems passing data in a Dictionary object to the CreateUserAndAccount method in a custom ExtendedMembershipProvider. In my Account controller, the (Post) Register method has the following:

Dictionary<string, object> userInfo = new Dictionary<string, object>();

userInfo.Add("Email", model.Email);
userInfo.Add("PasswordQuestion", model.PasswordQuestion);
userInfo.Add("PasswordAnswer", model.PasswordAnswer);

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, userInfo, true);

Which populates userInfo and succesfully calls the CreateUserAndAccount method in my custom provider.

I have two - maybe connected - problems.

Firstly, the method signatures are not the same, the provider method looks like this:

public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary<string, object> values) 

The Boolean and Dictionary parameters are switched over, but the method is still reached. If I change the code in the Account/Register method to match this I get:

The best overloaded method match for WebMatrix.WebData.WebSecurity.CreateUserAndAccount(string, string, object, bool)' has some invalid arguments.

I am confused as to how this can occur, my question is basically what on earth is going on?

Secondly, when the code reaches CreateUserAndAccount, the Dictionary object I passed to it has been converted to a RouteValueDictionary, all the other parameters appear as expected.

How can I get my Dictionary object back out and access the Email, PasswordQuestion, and PasswordAnswer values?

Upvotes: 0

Views: 1730

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31198

The signature of the static WebSecurity.CreateUserAndAccount method doesn't match the signature of the ExtendedMembershipProvider.CreateUserAndAccount method. You can't call one method using the other's signature, which is why you're getting the compiler error when you try.

The method on the WebSecurity class explicitly converts the propertyValues parameter to a RouteValueDictionary, because it's designed to accept any object, whereas the ExtendedMembershipProvider method expects an IDictionary<string, object> parameter.

For example, you could pass in an anonymous object and the call would still work:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
   new { model.Email, model.PasswordQuestion, model.PasswordAnswer },
   true);

Upvotes: 1

Related Questions