是否没有使用UserManager创建或更新Aspnetuser的缺点?

发布于 2025-01-21 11:36:21 字数 676 浏览 1 评论 0 原文

看来我在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();

或者这样做是完全可以的?

It seems that all the recommended approaches I see on the internet are to use the UserManager<TUser>.CreateAsync() method to create users.

What are the downside of using only ef core dbContext?

Say that I have a TimeZoneInfoId column in AspNetUsers.

 public class ApplicationUser : IdentityUser
    {
        public string? TimeZoneInfoId { get; set; }
    }

Is it bad to update it using dbContext?

For instance:

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();

Or is it perfectly fine to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-28 11:36:21

首先,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 your UserManager. 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 switch UserManager abstraction to another one - just like in case of IUserStore.

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.

日裸衫吸 2025-01-28 11:36:21

这是关于为什么我们选择使用 usermanager rolemanager 的解释。

asp.net核心身份:

是支持用户界面(UI)登录功能的API。

管理用户,密码,个人资料数据,角色,索赔,令牌,电子邮件
确认等等。

这不仅是数据库访问。这也是管理登录功能,安全令牌创建,安全密码管理等的代码。

如果您创建自定义系统,请考虑以上所有内容,请考虑一下解决方案的外部审核员(即使这是一个好主意),无论您做出什么选择),单位测试,性能测试

等以上已经完成。您也可以轻松地使用各种挂钩点自定义身份。

顺便说一句,Identity使用EF默认情况下访问数据存储。

构建您的多层应用程序,但要删除身份。这是一个横向关注的问题,在那里可以简化您的发展,并让您只担心业务需求。

在代码中定义 bad usermanager 为开发人员提供了更方便,高效和安全的选择,这是非常困难的。

请参阅 link

Here is an explain about why we choose to use UserManager and RoleManager.

ASP.NET Core Identity:

Is an API that supports user interface (UI) login functionality.

Manages users, passwords, profile data, roles, claims, tokens, email
confirmation, and more.

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, But userManager provides developers with a more convenient, efficient and safe choice.

refer to link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文