1

假设我有一个数据库,它有自己的 UserAccount、UserRole 和其他应用程序需要的表,我想知道我应该如何自定义 ASP.Net 身份验证以使用我自己创建的数据库表。ASP.Net Identity 首先使用代码,但我不是。我只是困惑我该怎么做。任何建议都会有所帮助。

提前致谢

4

1 回答 1

0

从 MembershipProvider 和 RoleProvider 类扩展。实施必要的方法。点配置以使用您的提供程序。

非常简单的方法是仅使用角色提供者

public class MyRoleProvider : RoleProvider
{
    public override bool IsUserInRole(string username, string roleName)
    {
        throw new NotImplementedException();
    }

    public override string[] GetRolesForUser(string username)
    {
        using (var userDao = new UserDao())
        {
            var user = userDao.GetUser(username);

            return user == null ? new string[0] : user.Roles
               .Select(r => r.Name).ToArray();
        }
    }

    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }

    public override bool RoleExists(string roleName)
    {
        throw new NotImplementedException();
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override string[] GetAllRoles()
    {
        throw new NotImplementedException();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        throw new NotImplementedException();
    }

    public override string ApplicationName { get; set; }
}

如果是 MVC ,这是我的AccountController :

public class AccountsController : Controller
{
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(LoginPage loginPage)
    {
        if (ModelState.IsValid)
        {
            using (var userDao = new UserDao())
            {
                var user = userDao.GetUser(loginPage.Login);
                if (user != null && user.Password == loginPage.Password)
                {
                    FormsAuthentication.SetAuthCookie(loginPage.Login, loginPage.RememberMe);
                    return RedirectToAction("Index", "Campaigns");
                }
            }
        }

        return View(loginPage);
    }

    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Login");
    }
}

型号(以防万一):

public class LoginPage
{
    [Required(ErrorMessage = "Enter login")]
    public string Login { get; set; }

    [Required(ErrorMessage = "Enter password")]
    public string Password { get; set; }

    public bool RememberMe { get; set; }
}

配置(内部):

<authentication mode="Forms">
  <forms loginUrl="/Accounts/Login" defaultUrl="/Campaigns/Index" />
</authentication>

<authorization>
  <deny users="?" />
</authorization>

<roleManager enabled="true" defaultProvider="CustomRoleProvider">
  <providers>
    <clear />
    <add name="CustomRoleProvider" type="BellIntegrator.OsmSkyMobile.Web.Helpers.SkyRoleProvider" />
  </providers>
</roleManager>
于 2013-11-15T09:36:00.823 回答