ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)
開發環境:vs2017 版本:15.3.5
項目環境:.net framework 4.6.1 模板asp.net core 2.0 Web應用程序(模型視圖控制器)
身份驗證:個人用戶賬號 存儲應用內的用戶帳戶
因為本人並不涉及開發一些中、大規模的應用,所以習慣使用本地數據庫,而不是數據庫服務,為了方便管理,所以本人的所有項目都是離線數據庫文件存儲(.mdf)。
下面開始:
一、修改數據庫連接。
1、相對路徑:
修改appsettings.json文件中的"ConnectionStrings"(第3行)
"DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true”
需註意的是:AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;
使用 ContentRootPath 是將文件放置在項目目錄下而不是wwwroot目錄下,這樣更安全。
ContentRootPath 用於包含應用程序文件。
WebRootPath 用於包含Web服務性的內容文件。
實際使用區別如下:
ContentRoot: C:\MyApp\
WebRoot: C:\MyApp\wwwroot\
2、修改Startup.cs
自動生成的原始代碼:
public class Startup {View Codepublic Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(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 void Configure(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?}"); }); } }
修改後的代碼:
①修改Startup方法為如下
public Startup(IConfiguration configuration,IHostingEnvironment env) { Configuration = configuration;
//新添加 _env = env; }
②添加public IHostingEnvironment _env { get; }
③修改ConfigureServices方法
註銷掉原有的services.AddDbContext
//添加修改()聲明變量conn並做相應處理
string conn = Configuration.GetConnectionString("DefaultConnection");
if (conn.Contains("%CONTENTROOTPATH%"))
{
conn = conn.Replace("%CONTENTROOTPATH%", _env.ContentRootPath);
}
//修改默認的連接服務為conn
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(conn));
修改完成後的代碼:
public class Startup { public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; //新添加 _env = env; } public IConfiguration Configuration { get; } //新添加 public IHostingEnvironment _env { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //services.AddDbContext<ApplicationDbContext>(options => // options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //添加修改()聲明變量conn並做相應處理 string conn = Configuration.GetConnectionString("DefaultConnection"); if (conn.Contains("%CONTENTROOTPATH%")) { conn = conn.Replace("%CONTENTROOTPATH%", _env.ContentRootPath); } //修改默認的連接服務為conn services.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 void Configure(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、手動在項目中添加“App_data”文件夾,並復制粘貼一個標準的內容為空的.mdf文件。
為方便大家學習我這裏為大家提供了示例數據庫。
ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)