1

比方說,我有一個數據庫擁有自己的UserAccount,UserRole和其他應用程序所需的表,我想知道我應該如何定製ASP.Net身份驗證與我自己的工作創建數據庫表。 ASP.Net Identity首先使用代碼,但我不是。我只是困惑,我該怎麼做。任何建議都會有幫助。自定義ASP.net身份使用現有的數據庫

在此先感謝

回答

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

這裏是我的的AccountController如果是MVC:

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> 
+0

感謝您的回覆,但我不希望使用ASP.Net會員,有一個叫ASP.Net身份的新框架,它具有不老會員的限制。 – saber

+0

@SaberAmani好的,讓我檢查一下 – Andrei

相關問題