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

本文关键字:身份验证 创建 表单 NET MVC 页面 ASP 通过 | 更新日期: 2023-09-27 18:13:10

我正在内部网上工作,到目前为止,我已经从Windows身份验证切换到表单身份验证,我可以连接/注册等。

但这是我应该做的:我希望链接员工列表(具有许多参数,例如登录名、密码、姓名等(,而不是能够通过通常的表单路由创建和帐户,并能够在创建新员工时创建新用户。

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

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

如何通过员工创建面板创建帐户,并基本上将通常的用户列表替换为员工列表?

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

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

private ApplicationDbContext db = new ApplicationDbContext();

但是在这种情况下,在我的示例中,它说。 context,只需将context更改为db,如果声明了ApplicationDbContext。

底部是同时创建用户类和员工类的示例。因此,在引用 Employee 类的同时向用户插入记录。但我想你现在已经明白了。

请注意,我没有向密码添加加密。因为这是一个全新的问题话题。

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