I recently completed a large order architecture with my friend: mvc5+ef6+Bootstrap, using vs2015 and the database is SQL Server2014. After the project is completed, I think there are many structures that I am worth learning from. I will summarize some experience here.
Create a project and delete the files and files in the App_Start directory at the beginning; clear the Modle folder, Controller folder and corresponding View; delete files and files in the directory
Modify the file (add <add key="owin:AutomaticAppStartup" value="false"/> to start the project without using the file)
<appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="owin:AutomaticAppStartup" value="false"/> <!--Remove the settings for the initial file creation--> </appSettings>
(No need to do it because the contents they come with are too redundant)
It is officially started to remove redundant content. First, we introduce the database. We can configure the database and can manually generate and modify it.
1. Want to create a Migrations folder in the project directory and add files to it
internal sealed class Configuration : DbMigrationsConfiguration<AccountContext> { public Configuration() { AutomaticMigrationsEnabled = true; ContextKey = ""; } protected override void Seed(AccountContext context) { //(context); } }
Add files in the Model folder
public class AccountContext:DbContext { public AccountContext():base("AccountContext") { } public DbSet<User> Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { <PluralizingTableNameConvention>(); } } <connectionStrings> <add name="AccountContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\;Initial Catalog=UserProject;Integrated Security=True" providerName="" /> </connectionStrings>
Then use the tools in vs2015 - NuGet Package Manager - Package Management Control Platform
Enter add-migration Initial Press Enter, enter update-database Press Enter. You will see the AccountContext database in the App_Data folder.
2. Add files in the Modle folder
public class User { public int ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public Role Role { get; set; } } public enum Role//Role enumeration{ administrator = 0, staff = 1, manager = 2, 总manager = 3, Chairman = 4 }
Add a file in the ViewModle folder
public class Account { [Required] public string Name { get; set; } [Required] public string Password { get; set; } public string RePassword { get; set; } }
Here we recommend that the Controller after creating BaseController is inherited and used.
public class BaseController : Controller { public string UserName => ; public AccountContext db = new AccountContext(); private User _userInfo = null; public User CurrentUserInfo { get { if (_userInfo == null) { var user = (u => == UserName);//In order not to access the user table every time, you can create a static class that stores user table information._userInfo = user == null ? null : new User() { ID = , UserName = , Role = }; } return _userInfo; } } //Verify role: Get the CustomAttributes of Action and filter rolesprotected override void OnActionExecuting(ActionExecutingContext filterContext) { (filterContext); var authRoleAtt = (false).SingleOrDefault(att => att is AuthorizeRoleAttribute) as AuthorizeRoleAttribute; if (authRoleAtt == null && CurrentUserInfo != null) return; if (!()) { = View("NoPermission", "_Layout", "You do not have permission to access this feature!"); } } //This is for loggingprotected override void OnActionExecuted(ActionExecutedContext filterContext) { (filterContext); var msg = $"user: {CurrentUserInfo?.UserName}, Link: {}"; if ( == "POST") msg += $", data: {(())}"; //(msg); } }
AdminController inherits BaseController
[Authorize] public ActionResult Index() { return View(()); } [Authorize, AuthorizeRole(Role.administrator)] public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(); } User user = (id); if (user == null) { return HttpNotFound(); } return View(user); }
Login page:
@model @{ = "Login"; } @using (("Login", "Admin",, new { @class = "form-horizontal", role = "form" })) { @() <hr /> @(true, "", new { @class = "text-danger" }) <div class="form-group"> @(m => , new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @(m => , new { @class = "form-control" }) @(m => , "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @(m => , new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @(m => , new { @class = "form-control" }) @(m => , "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Log in" class="btn btn-primary" /> </div> </div> }
Login Action:
[AllowAnonymous] public ActionResult Login() { return View(); } [HttpPost, AllowAnonymous] public ActionResult Login(Account model) { if () { var user = (t => == && == ); if (user != null) { (, false);//Put the username into the cookiereturn RedirectToAction("Index"); } else { ("Name", "The username does not exist!"); } } return View(model); } public ActionResult LogOff() { (); return RedirectToAction("Login"); }
In the above method, you must be in the administrator role when accessing the Details Action.
The above is the implementation of identity authentication and permission management based on the mvc5+ef6+Bootstrap framework introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!