Development environment: VS2017 version: 15.3.5
Project environment:. NET Framework 4.6.1 template ASP. 2.0 Web Application (Model View Controller)
Authentication: User accounts in the personal user account store app
Because I do not involve the development of some medium and large-scale applications, it is customary to use the local database, rather than database services, in order to facilitate management, so I all projects are offline database file storage (. mdf).
Start here:
First, modify the database connection.
1. Relative path:
Modify "ConnectionStrings" (line 3rd) in the Appsettings.json file
"DefaultConnection": "Data source= (localdb) \\mssqllocaldb; Attachdbfilename=%contentrootpath%\\app_data\\aspnet123.mdf;integrated security=true; Connect timeout=30; Multipleactiveresultsets=true "
Note that: attachdbfilename=%contentrootpath%\\app_data\\aspnet123.mdf;
Using Contentrootpath is more secure by placing files under the project directory instead of the Wwwroot directory.
Contentrootpath is used to include application files.
Webrootpath is used for content files that contain Web service.
The actual use difference is as follows:
Contentroot:c:\myapp\
Webroot:c:\myapp\wwwroot\
2, modify the Startup.cs
Auto-generated original code:
Public classStartup { PublicStartup (iconfiguration configuration) {Configuration=configuration; } PublicIConfiguration Configuration {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {services. Adddbcontext<ApplicationDbContext> (options =options. Usesqlserver (Configuration.getconnectionstring ("defaultconnection"))); Services. Addidentity<applicationuser, identityrole>() . Addentityframeworkstores<ApplicationDbContext>() . Adddefaulttokenproviders (); //ADD application services.Services. Addtransient<iemailsender, emailsender>(); Services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); App. Usebrowserlink (); App. Usedatabaseerrorpage (); } Else{app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); App. Useauthentication (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); } }
View Code
The Modified code:
① Modify the Startup method as follows
Public Startup (iconfiguration configuration,ihostingenvironment env) { configuration = Configuration;
New Add _env = env; }
② Add public ihostingenvironment _env {get;}
③ Modifying the Configureservices method
Log out of the existing services. Adddbcontext
Add modify () DECLARE variable conn and do the corresponding processing
String conn = configuration.getconnectionstring ("defaultconnection");
IF (Conn. Contains ("%contentrootpath%"))
{
conn = conn. Replace ("%contentrootpath%", _env. Contentrootpath);
}
Modify the default connection service to Conn
Services. adddbcontext<applicationdbcontext> (options =
Options. Usesqlserver (conn));
After modifying the completed code:
Public classStartup { PublicStartup (iconfiguration configuration, ihostingenvironment env) {configuration=configuration; //New Additions_env =env; } PublicIConfiguration Configuration {Get; } //New Additions PublicIhostingenvironment _env {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {//Services. adddbcontext<applicationdbcontext> (options =//options. Usesqlserver (configuration.getconnectionstring ("defaultconnection")); //Add modify () DECLARE variable conn and do the corresponding processing stringconn = Configuration.getconnectionstring ("defaultconnection"); if(Conn. Contains ("%contentrootpath%") ) {conn= Conn. Replace ("%contentrootpath%", _env. Contentrootpath); } //Modify the default connection service to ConnServices. adddbcontext<applicationdbcontext> (options =options. Usesqlserver (conn)); Services. Addidentity<applicationuser, identityrole>() . Addentityframeworkstores<ApplicationDbContext>() . Adddefaulttokenproviders (); //ADD application services.Services. Addtransient<iemailsender, emailsender>(); Services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); App. Usebrowserlink (); App. Usedatabaseerrorpage (); } Else{app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); App. Useauthentication (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); } }
View Code
3. Manually add the "App_Data" folder to the project and copy and paste a standard empty. mdf file.
For the convenience of everyone to learn I have provided a sample database for everyone.
Basic use of the ASP. NET identity 2.0.0 in ASP. NET Core 2.0.0 (i)