user2787570
user2787570

Reputation: 1

Registration Form using AccountModel.cs with another models MVC3

I want find a solution to my LoginForm.

could you tell me how i can join my models (ex. Doctor.cs) to AccountModel.cs (MVC Application Security - LogOn,Register or 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));
            }
        }

and this is Doctor.cs

public class Doc {

    public int DocId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DoktorArtUrl { get; set;

}

Help me , i just want that When i create acount , i create new Doctor in DB and create new user in MVC Security. Also tell me how i can do Profile View (shows values from Docs table who is combined with Security ( after LoGIN )

PS. Sorry For my English

Upvotes: 0

Views: 880

Answers (1)

Bastaspast
Bastaspast

Reputation: 1040

I would suggest to use the built-in lightweight SimpleMembership authorization. It will automatically provide the Asp.NET membership tables and a specific UserProfile (or Doctor) table to store other relevant properties.

This comes out of the box with the MVC 4 internet application template from Visual Studio

I suggest the following:

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

This is to initialize the simplemembership authorization on your internet application from application_start method in your global.asax.

As you can see the table/class that is used to store the relevant user information is called "UserProfile" by default. Just change the "UserProfile" (account models) class through Entity Framework code first and it will automatically adjust your data model.

Example given:

[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
}

I hope this information can point you in the right direction. If you have any remarks, feel free to ask.

Upvotes: 1

Related Questions