简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM