自定义表单身份验证MVC?
我开发了 MVC Web 应用程序。现在,我想在其中使用自定义表单身份验证。 为简单起见,我使用 SQL 会员资格提供程序进行注册。但是,我不想创建其他自定义表并将成员资格表映射到该表,因为我只需要创建简单的模块,我需要坚持使用默认表
我已经使用 Aspnet_regsql 工具生成了 sql 成员资格表现
在在 AccountModel.cs 中进入 RegisterModel 类我有添加了另外两个字段,例如:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last name")]
public string LastName { get; set; }
}
还更新了 dbo.aspnet_Membership 表并添加了另外两个字段名字和姓氏
现在在 AccountController.cs 中的控制器内 代码:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
}
}
这里因为 CreateUser 是默认的成员资格方法,所以我无法在创建用户时将自定义字段(名字和姓氏)插入表中?
我该怎么做?
I have developed MVC web app. Now, I want to use custom form authentication into it.
For simplicity I'm using SQL membership provider for registration stuff. But, I dont want to create other custom table and map membership table to that because I just need to create simple module i need to stick with default tables
I have generated sql membership table using Aspnet_regsql tool
Now in AccountModel.cs into RegisterModel class i have added two more fields like :
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last name")]
public string LastName { get; set; }
}
Also updated dbo.aspnet_Membership table and added two more fields firstname and lastname
Now inside Controllers in AccountController.cs
code :
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
}
}
Here as CreateUser is default method of membership so I'm not able to insert my custom fields (firstname and lastname )into table at the time of create user ?
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要扩展成员资格表。只是不要这样做。不。我是认真的。不要这样做。真的。我是认真的。不要这样做。
相反,请在应用程序中创建一个用户表,将其主键设置为 GUID,然后添加自定义字段。创建用户时,将新记录插入用户表中,并使用从 CreateUser 返回的 MembershipUser 的 ProviderUserKey。这非常简单,并且不需要搞乱会员系统。
我知道你说过你不想这样做,但这是唯一的好方法。
唯一的其他选择是使用配置文件提供程序来保存此信息,但是您将无法将其映射到其他任何内容。 Profile 提供程序非常通用,大多数人发现它没有那么有用。
如果你修改成员资格表,你就会破坏一切。正如您所发现的,如果不创建自己的自定义提供程序,就没有处理数据的好方法。
Don't extend the membership tables. Just don't do it. Don't. I meant it. Don't do it. Really. I'm serious. Don't do it.
Instead, create a Users table in your app, make it's primary key a GUID, and add your custom fields. When you create the user, insert a new record into your users table and use the ProviderUserKey of the MembershipUser returned from CreateUser. This is extremely simple, and does not require messing with the membership system.
I know you said you don't want to, but that's the only good way to do it.
The only other alternative is to use the Profile provider to save this information, however you won't be able to map it to anything else. The Profile provider is pretty generic and most people find it to be not that useful.
If you modify the membership tables, you will break things. And there's no good way to deal with the data, as you've found out, without creating your own custom provider.
Ashu,在阅读了您的要求后,我觉得您应该为自己编写一个自定义提供程序,而不是坚持使用默认提供程序。我这么说的原因是,您手头上实现您的要求的唯一选择是遵循@Mystere Man 的解决方案。
现在,如果您打算与会员提供商合作,那么为什么不为您自己创建一个全新的提供商。这并不像人们想象的那么困难。您需要做的就是实现一些抽象类来获得相应的东西。
网上有大量教程可用于从头开始构建自定义会员提供商,我在下面列出了其中的一些。
Ashu, after reading your requirements, i feel you should go with coding a custom provider for yourself rather than sticking with the default. The reason I say this is that the only option you have on-hand to achieve your requirements is by following @Mystere Man's solution.
Now, if you are going to play with the membership provider, than why not make an entirely new provider for your own. It isn't that difficult as people think it is. All you need to do is implement some abstract classes to have things accordingly.
There are tons of tutorial available online for building a custom membership provider from scratch and i am listing few them below.