我想找一个我的LoginForm的解决方案。
你能告诉我如何将我的模型(例如Doctor.cs)加入AccountModel.cs(MVC Application Security - LogOn,Register或ChangePassword)。
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, "question", "answer", true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
MigrateShoppingCart(model.UserName);
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
这是Doctor.cs
公共课文件 {
public int DocId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DoktorArtUrl { get; set;
}
帮助我,我只是想要当我创建帐户时,我在DB中创建新的Doctor并在MVC Security中创建新用户。还告诉我如何进行个人资料视图(显示与安全性结合的文档表中的值(在LoGIN之后)
PS。对不起我的英文
答案 0 :(得分:1)
我建议使用内置的轻量级SimpleMembership授权。它将自动提供Asp.NET成员资格表和特定的UserProfile(或Doctor)表来存储其他相关属性。
这是开箱即用的Visual Studio
的MVC 4互联网应用程序模板我建议如下:
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
public class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
using (var context = new UsersContext())
context.UserProfiles.Find(1);
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Doctor", "DocId", "UserName", autoCreateTables: true);
}
}
这是从global.asax中的application_start方法初始化Internet应用程序上的simplemembership授权。
正如您所看到的,默认情况下,用于存储相关用户信息的表/类称为“UserProfile”。只需首先通过Entity Framework代码更改“UserProfile”(帐户模型)类,它将自动调整您的数据模型。
给出的例子:
[Table("Doctor")]
public class UserProfile // can also be public class Doctor
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int DocId{ get; set; }
public string UserName { get; set; }
//other relevant properties
public string FirstName { get; set; }
public string LastName { get; set; }
public string DoktorArtUrl { get; set;
//etc
}
我希望这些信息可以指出你正确的方向。如果您有任何评论,请随时提出。