ASP.NET MVC 5 + Entity Framework 5 + Windows Authentication – System Access Right ( Membership Design Pattern )
Create SQL Table Statement |
CREATE TABLE [dbo].[SystemUser] ( [ID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [UserID] [nvarchar](max) NOT NULL, [Role] [nvarchar](1) NOT NULL ) |
SystemUser.cs ( Project : InventoryDAO / Entity Framework – Model Layer / generated from Entity Framework ) |
namespace InventoryDAO { using System; using System.Collections.Generic; public partial class SystemUser { public int ID { get; set; } public string UserID { get; set; } public string Role { get; set; } } } |
DAO\Inventory\AuthDAO.cs ( Project : InventoryDAO / Business Logic Layer ) |
using System; using System.Linq; namespace InventoryDAO.DAO.Inventory { public class AuthDAO { private InventoryEntities entity; public AuthDAO() { this.entity = new InventoryEntities(); } public bool isAuth(string LoginName) { if (this.entity.SystemUsers.Where(x => x.UserID.ToUpper() == LoginName.ToUpper()).FirstOrDefault() != null) return true; else return false; } } } |
Controllers\InventoryController.cs ( Project : InventoryWeb / Controller Layer ) |
public class InventoryController : Controller { … … private string LoginName; private bool isAuth; private InventoryDAO.DAO.Inventory.AuthDAO Auth; public InventoryController() { … … this.Auth = new InventoryDAO.DAO.Inventory.AuthDAO(); this.LoginName = System.Web.HttpContext.Current.User.Identity.Name; this.isAuth = this.Auth.isAuth(this.LoginName); } public ActionResult Index() { if (this.isAuth) return View(); else return View("AccessDenied"); } public ActionResult AccessDenied() { return View(); } … … } |