通过表单身份验证创建帐户 ASP.NET MVC 页面

Account creation via Form authentication ASP.NET MVC page

我在内联网上工作,到目前为止我已经从Windows身份验证切换到表单身份验证并且我能够连接/注册等。

但这是我应该做的事情:我希望link员工列表[=29=,而不是通过通常的表格路线创建和帐户](具有 许多参数,例如登录名、密码、姓名等)并且能够在我创建新员工时创建新用户。

这是我的员工创建控制器:

public ActionResult Create()
    {
        ViewBag.CompanyList = _service.ListCompany();
        ViewBag.SupervisorList = _service.ListSupervisor();

        return View();
    }

    //
    // POST: /Employee/Create

    [HttpPost]
    public ActionResult Create([Bind(Exclude = "Id")] Employee objEmployee, FormCollection form)
    {            
        ViewBag.CompanyList = _service.ListCompany();
        ViewBag.SupervisorList = _service.ListSupervisor();

        objEmployee.CreatedDate = System.DateTime.Now;
        objEmployee.UpdatedDate = System.DateTime.Now;
        objEmployee.CompanyId = int.Parse(form["CompanyId"]);
        objEmployee.Supervisor = form["Supervisor"];

        if (_service.Create(objEmployee))
            return new RedirectResult(Url.Action("Index"));

        else
        {
            if (!_service.Validate(objEmployee))
                return View();
            else
                return new RedirectResult(Url.Action("Index", "Error", new { Message = "Error Create Employee", NextPage = CRAWebSiteMVC.Properties.Resources.Employee + @"/" + CRAWebSiteMVC.Properties.Resources.Create }));
        } 
    }

这里是常用方式,我通过正常形式的身份验证创建帐户。注册:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            [...]
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

如何通过员工创建面板创建一个帐户,并基本上用员工列表替换普通用户列表?

现在通常上下文在控制器中自动声明为 db <-。 它被声明为

private ApplicationDbContext db = new ApplicationDbContext();

但在我的例子中,这个案例是这样说的。 context 如果声明了 ApplicationDbContext,只需将 context 更改为 db

下面是同时创建用户class和员工class的例子。因此,在引用 Employee class 的同时向 User 插入一条记录。但我想你现在明白了。

请注意,我没有对密码进行加密。因为这是一个全新的问题。

var userStore = new UserStore<ApplicationUser>(context);
        var userManager = new UserManager<ApplicationUser>(userStore);



        if (!context.Users.Any(t => t.UserName == "admin@Konek.com"))
        {
            var user = new ApplicationUser { UserName = "admin@Konek.com", Email = "admin@Konek.com" };
            userManager.Create(user, "Password1!");

            context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Admin" });
            context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Receptionist" });
            context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Security" });
            context.SaveChanges();

            userManager.AddToRole(user.Id, "Admin");

            Employee admingEmployee = new Employee
            {
                Age=25,
                Citizenship="Filipino",
                CivilStatus=CivilStatus.Single,
                DateOfBirth=DateTime.Now,
                EmailAddress="admin@Konek.com",
                FirstName="Admin",
                Gender=Gender.Male,
                HomeAddress="None",
                LandLine="531-5555",
                LastName="Administrator",
                MiddleName="admin",
                MobileNumber="09275225222",
                Photo = "*******",
                PlaceofBirth="*****",
                Password = "********",
                Role=Role.Admin
            };

            context.Employees.Add(admingEmployee);
            context.SaveChanges();