.net core2.0下使用Identity改用dapper存儲數據

目錄html

  1. 前言
  2. 使用dapper作數據層調用
  3. 使用T4模板生成
  4. 使用緩存
  5. 使用swagger作接口文檔(非restful)
  6. 使用identity作身份認證
  7. 使用jwt作身份認證
  8. 使用CORS跨域
  9. 調用webservice服務
  10. 接入支付寶與微信支付
  11. 使用epplus. core操做excel

前言、git

  已經好多天沒寫博客了,鑑於空閒無聊之時又興起想寫寫博客,也當是給本身作個筆記。過了這麼些天,個人文筆仍是依然那麼爛就請多多諒解了。今天主要是分享一下在使用.net core2.0下的實際遇到的狀況。在使用webapi時用了identity作用戶驗證。官方文檔是的是用EF存儲數據來使用dapper,由於我的偏好緣由因此不想用EF。因而乎就去折騰。改爲使用dapper作數據存儲。因而就有了如下的經驗。web

1、使用Identity服務ajax

  先找到Startup.cs 這個類文件 找到 ConfigureServices 方法sql

services.AddIdentity<ApplicationUser, ApplicationRole>().AddDefaultTokenProviders();//添加Identity
services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>();
services.AddTransient<IRoleStore<ApplicationRole>, CustomRoleStore>();
string connectionString = Configuration.GetConnectionString("SqlConnectionStr");
services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));
services.AddTransient<DapperUsersTable>();

  而後在 Configure 方法 的 app.UseMvc() 前加入下列代碼,net core 1.0的時候是app.UseIdentity() 如今已經棄用改成如下方法。數據庫

//使用驗證
app.UseAuthentication();

  這裏的 ApplicationUser 是自定義的一個用戶模型 具體是繼承 IdentityUser 繼承它的一些屬性json

    public class ApplicationUser :IdentityUser
    {
        public string AuthenticationType { get; set; }
        public bool IsAuthenticated { get; set; }
        public string Name { get; set; }
    }

  這裏的 CustomUserStore 是自定義提供用戶的全部數據操做的方法的類它須要繼承三個接口:IUserStoreIUserPasswordStoreIUserEmailStoreapi

  IUserStore<TUser>接口是在用戶存儲中必須實現的惟一接口。 它定義了用於建立、 更新、 刪除和檢索用戶的方法。跨域

  IUserPasswordStore<TUser>接口定義實現以保持通過哈希處理的密碼的方法。 它包含用於獲取和設置工做通過哈希處理的密碼,以及用於指示用戶是否已設置密碼的方法的方法。緩存

  IUserEmailStore<TUser>接口定義實現以存儲用戶電子郵件地址的方法。 它包含用於獲取和設置的電子郵件地址和是否確認電子郵件的方法。

  這裏跟.net core 1.0的實現接口方式有點不一樣。須要多實現 IUserEmailStore 才能不報錯

  具體代碼以下。以供你們參考。

using Microsoft.AspNetCore.Identity;
using System;
using System.Threading.Tasks;
using System.Threading;

namespace YepMarsCRM.Web.CustomProvider
{
    /// <summary>
    /// This store is only partially implemented. It supports user creation and find methods.
    /// </summary>
    public class CustomUserStore : IUserStore<ApplicationUser>,
        IUserPasswordStore<ApplicationUser>,
        IUserEmailStore<ApplicationUser>
    {
        private readonly DapperUsersTable _usersTable;

        public CustomUserStore(DapperUsersTable usersTable)
        {
            _usersTable = usersTable;
        }

        #region createuser
        public async Task<IdentityResult> CreateAsync(ApplicationUser user,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return await _usersTable.CreateAsync(user);
        }
        #endregion

        public async Task<IdentityResult> DeleteAsync(ApplicationUser user,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return await _usersTable.DeleteAsync(user);

        }

        public void Dispose()
        {
        }

        public Task<ApplicationUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async Task<ApplicationUser> FindByIdAsync(string userId,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (userId == null) throw new ArgumentNullException(nameof(userId));
            Guid idGuid;
            if (!Guid.TryParse(userId, out idGuid))
            {
                throw new ArgumentException("Not a valid Guid id", nameof(userId));
            }

            return await _usersTable.FindByIdAsync(idGuid);

        }

        public async Task<ApplicationUser> FindByNameAsync(string userName,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (userName == null) throw new ArgumentNullException(nameof(userName));

            return await _usersTable.FindByNameAsync(userName);
        }

        public Task<string> GetEmailAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return Task.FromResult(user.Email);
        }

        public Task<bool> GetEmailConfirmedAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedEmailAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetPasswordHashAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return Task.FromResult(user.PasswordHash);
        }

        public Task<string> GetUserIdAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return Task.FromResult(user.Id.ToString());
        }

        public Task<string> GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));

            return Task.FromResult(user.UserName);
        }

        public Task<bool> HasPasswordAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetEmailAsync(ApplicationUser user, string email, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetEmailConfirmedAsync(ApplicationUser user, bool confirmed, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetNormalizedEmailAsync(ApplicationUser user, string normalizedEmail, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));
            if (normalizedEmail == null) throw new ArgumentNullException(nameof(normalizedEmail));

            user.NormalizedEmail = normalizedEmail;
            return Task.FromResult<object>(null);
        }

        public Task SetNormalizedUserNameAsync(ApplicationUser user, string normalizedName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));
            if (normalizedName == null) throw new ArgumentNullException(nameof(normalizedName));

            user.NormalizedUserName = normalizedName;
            return Task.FromResult<object>(null);
        }

        public Task SetPasswordHashAsync(ApplicationUser user, string passwordHash, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null) throw new ArgumentNullException(nameof(user));
            if (passwordHash == null) throw new ArgumentNullException(nameof(passwordHash));

            user.PasswordHash = passwordHash;
            return Task.FromResult<object>(null);

        }

        public Task SetUserNameAsync(ApplicationUser user, string userName, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<IdentityResult> UpdateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return _usersTable.UpdateAsync(user);
        }
    }
}
CustomUserStore

 

2、使用使用dapper作數據存儲

  接着就是使用dapper作數據存儲。該類的方法都是經過 CustomUserStore 調用去操做數據庫的。具體代碼以下。根據實際的用戶表去操做dapper便可。

using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using System.Threading;
using System.Data.SqlClient;
using System;
using Dapper;
using YepMarsCRM.Enterprise.DataBase.Model;
using YepMarsCRM.Enterprise.DataBase.Data;

namespace YepMarsCRM.Web.CustomProvider
{
    public class DapperUsersTable
    {
        private readonly SqlConnection _connection;
        private readonly Sys_AccountData _sys_AccountData;
        public DapperUsersTable(SqlConnection connection)
        {
            _connection = connection;
            _sys_AccountData = new Sys_AccountData();
        }

        private Sys_Account ApplicationUserToAccount(ApplicationUser user)
        {
            return new Sys_Account
            {
                Id = user.Id,
                UserName = user.UserName,
                PasswordHash = user.PasswordHash,
                Email = user.Email,
                EmailConfirmed = user.EmailConfirmed,
                PhoneNumber = user.PhoneNumber,
                PhoneNumberConfirmed = user.PhoneNumberConfirmed,
                LockoutEnd = user.LockoutEnd?.DateTime,
                LockoutEnabled = user.LockoutEnabled,
                AccessFailedCount = user.AccessFailedCount,
            };
        }

        #region createuser
        public async Task<IdentityResult> CreateAsync(ApplicationUser user)
        {
            int rows = await _sys_AccountData.InsertAsync(ApplicationUserToAccount(user));
            if (rows > 0)
            {
                return IdentityResult.Success;
            }
            return IdentityResult.Failed(new IdentityError { Description = $"Could not insert user {user.Email}." });
        }
        #endregion

        public async Task<IdentityResult> DeleteAsync(ApplicationUser user)
        {
            //string sql = "DELETE FROM Sys_Account WHERE Id = @Id";
            //int rows = await _connection.ExecuteAsync(sql, new { user.Id });

            int rows = await _sys_AccountData.DeleteForPKAsync(ApplicationUserToAccount(user));

            if (rows > 0)
            {
                return IdentityResult.Success;
            }
            return IdentityResult.Failed(new IdentityError { Description = $"Could not delete user {user.Email}." });
        }


        public async Task<ApplicationUser> FindByIdAsync(Guid userId)
        {
            string sql = "SELECT *  FROM Sys_Account WHERE Id = @Id;";
            return await _connection.QuerySingleOrDefaultAsync<ApplicationUser>(sql, new
            {
                Id = userId
            });
        }


        public async Task<ApplicationUser> FindByNameAsync(string userName)
        {
            string sql = "SELECT * FROM Sys_Account WHERE UserName = @UserName;";

            return await _connection.QuerySingleOrDefaultAsync<ApplicationUser>(sql, new
            {
                UserName = userName
            });

            //var user = new ApplicationUser() { UserName = userName, Email = userName, EmailConfirmed = false };
            //user.PasswordHash = new PasswordHasher<ApplicationUser>().HashPassword(user, "test");
            //return await Task.FromResult(user);
        }

        public async Task<IdentityResult> UpdateAsync(ApplicationUser applicationUser)
        {
            var user = ApplicationUserToAccount(applicationUser);
            var result = await _sys_AccountData.UpdateForPKAsync(user);
            if (result > 0)
            {
                return IdentityResult.Success;
            }
            return IdentityResult.Failed(new IdentityError { Description = $"Could not update user {user.Email}." });
        }
    }
}
DapperUsersTable

 

3、使用UserManager、SignInManager驗證操做

  新建一個 AccountController 控制器 並在構造函數中獲取 依賴注入的對象 UserManager 與 SignInManager 以下:

  [Authorize]
  public class AccountController : Controller { private readonly UserManager<ApplicationUser> _userManager; private readonly SignInManager<ApplicationUser> _signInManager; private readonly ILogger _logger; public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, ILoggerFactory loggerFactory) { _userManager = userManager; _signInManager = signInManager; _logger = loggerFactory.CreateLogger<AccountController>(); } }

  SignInManager 是提供用戶登陸登出的API ,UserManager 是提供用戶管理的API。

  接着來實現一下簡單的登陸登出。

        /// <summary>
        /// 登陸
        /// </summary>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login(ReqLoginModel req)
        {
            var json = new JsonResultModel<object>();
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(req.UserName, req.Password, isPersistent: true, lockoutOnFailure: false);
                if (result.Succeeded)
                {
                    json.code = "200";
                    json.message = "登陸成功";
                }
                else
                {
                    json.code = "400";
                    json.message = "登陸失敗";
                }
                if (result.IsLockedOut)
                {
                    json.code = "401";
                    json.message = "帳戶密碼已錯誤3次,帳戶被鎖定,請30分鐘後再嘗試";
                }
            }
            else
            {
                var errorMessges = ModelState.GetErrorMessage();
                json.code = "403";
                json.message = string.Join("", errorMessges);
            }
            return json.ToJsonResult();
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LogOut()
        {await _signInManager.SignOutAsync();
            var json = new JsonResultModel<object>()
            {
                code = "200",
                data = null,
                message = "登出成功",
                remark = string.Empty
            };
            return json.ToJsonResult();
        }

4、使用Identity配置

  在 ConfigureServices 方法中加入

            services.Configure<IdentityOptions>(options =>
            {
                // 密碼配置
                options.Password.RequireDigit = false;//是否須要數字(0-9).
                options.Password.RequiredLength = 6;//設置密碼長度最小爲6
                options.Password.RequireNonAlphanumeric = false;//是否包含非字母或數字字符。
                options.Password.RequireUppercase = false;//是否須要大寫字母(A-Z).
                options.Password.RequireLowercase = false;//是否須要小寫字母(a-z).
                //options.Password.RequiredUniqueChars = 6;

                // 鎖定設置
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);//帳戶鎖定時長30分鐘
                options.Lockout.MaxFailedAccessAttempts = 3;//10次失敗的嘗試將帳戶鎖定
                //options.Lockout.AllowedForNewUsers = true;

                // 用戶設置
                options.User.RequireUniqueEmail = false; //是否Email地址必須惟一
            });

            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                //options.Cookie.Expiration = TimeSpan.FromMinutes(30);//30分鐘
                options.Cookie.Expiration = TimeSpan.FromHours(12);//12小時
                options.LoginPath = "/api/Account/NotLogin"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
                //options.LogoutPath = "/api/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
                //options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
                options.SlidingExpiration = true;
            });

5、其餘

  在實現的過程當中遇到一些小情況。例如Identity不生效。是由於未在app.UseMvc() 以前使用形成的。 若是未登陸會形成跳轉。後來查看了.net core Identity 的源碼後 發現 若是是ajax狀況下 不會跳轉而時 返回401的狀態碼頁面。

而後就是Idenetity的密碼加密 是用 PasswordHasher 這個類去加密的。若是想用本身的加密方式。只能經過繼承接口去更改本來的方式。而後大體說到這麼些。也當是給本身作作筆記。作得很差請你們多給點意見。多多諒解。謝謝。

相關文章
相關標籤/搜索