是否没有使用UserManager创建或更新Aspnetuser的缺点?
看来我在Internet上看到的所有推荐方法都是使用 usermanager< tuser> .createasync()
创建用户的方法。
仅使用EF Core DbContext的缺点是什么?
假设我在Aspnetusers中有一个 TimeZoneInfoid
列。
public class ApplicationUser : IdentityUser
{
public string? TimeZoneInfoId { get; set; }
}
使用DBContext更新它是否不好?
例如:
using var dbContext = await DbContextFactory.CreateDbContextAsync();
var userToUpdate = await dbContext.ApplicationUsers.Where(u => u.UserName == "John").FirstOrDefaultAsync();
userToUpdate?.TimeZoneInfoId = "Samoa Standard Time";
await dbContext.SaveChangesAsync();
或者这样做是完全可以的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,Usermanger是另一个抽象。通常,它与基于EF的用户商店一起使用,但是您可以实现所需的任何商店。
因此,如果您在整个项目范围内使用UseManager,并且在某个开发阶段,您决定要将当前基于EF的用户存储转换为其他东西,唯一要做的就是在
iuserstore > usermanager 。如果您按照提供的方式(直接调用DB) - 您应该在管理用户管理的每个地方重构。
用户管理器要注意更多事情,例如:更新安全邮票 /归一化或验证 - 非常重要的是,您可以修改
usermanager < / code>的各个方面 - 您唯一要做的就是要切换
usermanager
抽象到另一个 - 就像iuserstore
一样。总而言之,
usermanager
工作就像许多组件的好胶水,使您可以管理用户。默认情况下,它使用良好的默认实现,但是很容易将其调整您需要的任何方式。First of all, UserManger is another level of abstraction. Usually it's used with EF-based user store, but you may implement ANY store you want.
So if you use UseManager project-wide and at some development stage you decide that you want to switch current EF-based user store to something else, the only thing to do is to replace
IUserStore
in yourUserManager
. If you go the way you provided (calling db directly) - you'll be supposed to refactor EVERY place where you managed the user.User manager takes care about few more things, for example: updating security stamps / normalization or validation - it's very important to know, that you can modify every aspect of
UserManager
- the only thing you have to do is to switchUserManager
abstraction to another one - just like in case ofIUserStore
.To sum up,
UserManager
work's like a good glue for many components which allows you to manage users. In default it uses good default implementations but it's very easy to adjust it any way you need.这是关于为什么我们选择使用
usermanager
和rolemanager
的解释。这不仅是数据库访问。这也是管理登录功能,安全令牌创建,安全密码管理等的代码。
如果您创建自定义系统,请考虑以上所有内容,请考虑一下解决方案的外部审核员(即使这是一个好主意),无论您做出什么选择),单位测试,性能测试
等以上已经完成。您也可以轻松地使用各种挂钩点自定义身份。
顺便说一句,Identity使用EF默认情况下访问数据存储。
构建您的多层应用程序,但要删除身份。这是一个横向关注的问题,在那里可以简化您的发展,并让您只担心业务需求。
在代码中定义
bad
,usermanager
为开发人员提供了更方便,高效和安全的选择,这是非常困难的。请参阅 link
Here is an explain about why we choose to use
UserManager
andRoleManager
.It's not just database access. It is also code that manages login functionality, secure token creation, secure password management and much more.
You need to take all of the above into consideration if you create a custom system, have an external auditor to pen-test your solution (even though this is a good idea whatever choice you make), unit test, performance test etc.
All the above is already done. You can easily customize the identity with various hook points too.
BTW, identity uses ef to access the datastore already by default.
Do structure your multilayer application, but leave identity out of it. It is a horizontal concern and it's presence is there to simplify your development and let you worry about your business needs only.
It's very difficlut to define
bad
in your code, ButuserManager
provides developers with a more convenient, efficient and safe choice.refer to link