ASP.NET Custom MembershipProvider, change CreateUser semantics or add a new Method
Is there any way to add additional method let say with semantic like that:
CreateUser(User user, UserInfo userInfo, IsInRole isInRole, <any another things here>)
I may create the custom method and call that method separately, but in order to keep the sing开发者_Go百科s consistently i just wondering is there any way to customize MembershipProvider
like that or to do something similar?
I suppose you already have your custom membership provider created. You can simply add that method there and then use it in your application. Let's say that you have custom membership provider like this pseudo (simplified) code:
public class CustomMP : MembershipProvider
{
var db = // your data (member) storage
public void CreateUser(string username, UserInfo userInfo, /* etc... */)
{
// process data
// create new user instance.. like:
MyUser newUser = new MyUser(/* params */);
db.AppUsers.Add(newUser);
db.SaveChanges();
}
}
Then you can use it in your account controller (or anywhere you want..)
public ActionResult CreateMyUser(SomeModel model)
{
if (ModelState.IsValid)
{
// create instance of your custom mebership provider
// (unless you already have one somewhere else ... as you should)
CustomMP cmp = new CustomMP();
// call your new method
cmp.CreateUser(model.username, model.userInfo, /* etc.. */);
// login newly created user (optional)
FormsAuthentication.SetAuthCookie(model.username, false /* = don't create persistent cookie);
return RedirectToAction("Index","Home");
}
}
This example is simplified and missing any form of validation, but I think you can get the idea from it. It shows that you should be able to extend your custom membership provider any way you want.
精彩评论