【问题标题】:Account creation via Form authentication ASP.NET MVC page通过表单身份验证 ASP.NET MVC 页面创建帐户
【发布时间】:2015-09-17 07:43:01
【问题描述】:

我正在使用 Intranet,到目前为止,我已从 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);
    }

我怎样才能通过员工创建面板创建一个帐户,并基本上用员工列表替换通常的用户列表?

【问题讨论】:

  • 您的意思是同时创建一个电子邮件模型。将其添加到 UserStore 和 UserManager 类中?我希望您知道您可以使用 UserStore 和 UserManager 类来创建您所说的“普通用户”。
  • 作为新技术的新手,我没有。我会调查的。

标签: c# asp.net-mvc sql-server-2008 forms-authentication


【解决方案1】:

现在通常上下文,在控制器中自动声明为 db

private ApplicationDbContext db = new ApplicationDbContext();

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

底部是一个同时创建一个User类和一个Employee类的例子。因此,在引用 Employee 类的同时向 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();

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2015-04-20
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多