设为首页 加入收藏

TOP

asp.net core系列 46 Identity介绍(一)
2019-09-17 18:35:27 】 浏览:42
Tags:asp.net core 系列 Identity 介绍

一. Identity 介绍

  ASP.NET Core Identity是一个会员系统,可为ASP.NET Core应用程序添加登录功能。可以使用SQL Server数据库配置身份以存储用户名,密码和配置文件数据。或者,可以使用另一个持久性存储,例如,Azure表存储。下面学习如何使用Identity注册,登录以及基架标识。

  

  1.1 Identity搭建演示

    下面使用vs 2017来演示:

      1.选择“文件” > “新建” > “项目”。

      2.选择“ASP.NET Core Web应用程序”。 将项目命名WebAppIdentityDemo具有项目下载相同的命名空间。 单击 “确定”。

      3.选择 ASP.NET Core Web MVC应用程序,然后选择更改身份验证。

      4.选择单个用户帐户然后单击确定。

    生成的项目包含了Identity会员系统,目录结构如下所示,生成后的目录结构有疑惑,怎么没看见Identity会员系统相关的model, Controller,cshtml等文件,继续往下了解。

    (1) 修改连接字符串

      找到appsettings.json文件,修改ConnectionStrings的数据库连接字符串, 默认是连接本机sql server数据库,我改成了连接远程数据库。

"ConnectionStrings": {
    "DefaultConnection": "Data Source = 172.168.16.75;Initial Catalog =IdentityDB; User ID = hsr;Password =js*2015;"
  },

     (2) 基于生成的迁移代码,同步到数据库 

      PM> Update-Database 

    (3) 配置Identity服务

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity
<IdentityUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); services.Configure<IdentityOptions>(options => { // Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.ConfigureApplicationCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); options.LoginPath = "/Identity/Account/Login"; options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.SlidingExpiration = true; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }

    (4) 确认调用UseAuthentication中间件

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
  app.UseAuthentication();
    app.UseMvc();
}

     (5) 启动程序,首页提供了注册,登录的链接

      注册成功后/Identity/Account/Register,在数据库中AspNetUsers表会新增一条数据(密码:Asp.netCore123)。注册成功后,说明数据库连

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.net c#将数据库数据对象转换为实.. 下一篇一道有意思的多线程面试题 C# 代..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目