没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
全文可编辑 word 文档 页眉与背景水印可删除
1
.net core2.0 下使用 Identity 改用 dapper
存储数据(实例讲解)
前言、
已经好多天没写博客了,鉴于空闲无聊之时又兴起想写
写博客,也当是给自己做个笔记。过了这么些天,我的文笔
还是依然那么烂就请多多谅解了。今天主要是分享一下在使
用.net core2.0 下的实际遇到的情况。在使用 webapi 时用
了 identity 做用户验证。官方文档是的是用 EF 存储数据来
使用 dapper,因为个人偏好原因所以不想用 EF。于是乎就
去折腾。改成使用 dapper 做数据存储。于是就有了以下的
经验。
一、使用 Identity 服务
先 找 到 Startup.cs 这 个 类 文 件 找 到
ConfigureServices 方法
services.AddIdentity<ApplicationUser,
ApplicationRole>().AddDefaultTokenProviders();// 添
加 Identity
services.AddTransient<IUserStore<ApplicationUser>,
CustomUserStore>();
全文可编辑 word 文档 页眉与背景水印可删除
2
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 继承它的一些属性
public class ApplicationUser :IdentityUser
{
public string AuthenticationType { get; set; }
public bool IsAuthenticated { get; set; }
public string Name { get; set; }
}
全文可编辑 word 文档 页眉与背景水印可删除
3
这里的 CustomUserStore 是自定义提供用户的所有数
据操作的方法的类它需要继承三个接口:IUserStore ,
IUserPasswordStore,IUserEmailStore
IUserStore<TUser>接口是在用户存储中必须实现的唯
一接口。 它定义了用于创建、 更新、 删除和检索用户的方
法。
IUserPasswordStore<TUser>接口定义实现以保持经过
哈希处理的密码的方法。 它包含用于获取和设置工作经过哈
希处理的密码,以及用于指示用户是否已设置密码的方法的
方法。
IUserEmailStore<TUser>接口定义实现以存储用户电
子邮件地址的方法。 它包含用于获取和设置的电子邮件地址
和是否确认电子邮件的方法。
这里跟.net core 1.0 的实现接口方式有点不同。需要多
实现 IUserEmailStore 才能不报错
具体代码如下。以供大家参考。
CustomUserStore
using Microsoft.AspNetCore.Identity;
using System;
using System.Threading.Tasks;
using System.Threading;
全文可编辑 word 文档 页眉与背景水印可删除
4
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>
全文可编辑 word 文档 页眉与背景水印可删除
5
CreateAsync(ApplicationUser user,
CancellationToken cancellationToken =
default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequ
ested();
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.ThrowIfCancellationRequ
ested();
if (user == null) throw new
剩余28页未读,继续阅读
资源评论
mmoo_python
- 粉丝: 0
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功