.net 5 在启动时将 AutoSaveChanges 设置为 false [英] .net 5 Set AutoSaveChanges to false in Startup

查看:34
本文介绍了.net 5 在启动时将 AutoSaveChanges 设置为 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .net 5 和身份,我无法弄清楚如何在我的启动文件上设置 AutoSaveChanges.如果在注册或登录代码期间出现问题,我需要对我的用户表进行大量清理.

Using .net 5 and identity, I cannot figure out how to set AutoSaveChanges on my startup file. If something fails during register or login code, i need to do a lot of cleanup on my user tables.

services.AddDefaultIdentity<MyUser()
.AddRoles<IdentityRole().AddEntityFrameworkStores<MyDbContext>();

我在 防止 ASP.NET Identity 的 UserManager 中看到一个答案保存,但不知道这与我的启动服务有什么关系.

I see an answer in Prevent ASP.NET Identity's UserManager from automatically saving, but do not know how this relates to my startup service.

推荐答案

找到了一个仍然适用于 .NET 5 的旧帖子.

Found an older post that still works for .NET 5.

要在 UserManager 中关闭 AutoSave,您需要注册一个自定义 UserStore.为此,请创建一个新文件:

To turn off AutoSave in UserManager, you need to register a custom UserStore. To do this, create a new file:

public class CustomUserStore : UserStore<IdentityUser>  // important to use custom Identity user object here!  
    {
        public CustomUserStore(AppDbContext context)
        : base(context)
        {
            AutoSaveChanges = false;
        }
    }

在 Startup.cs 中,通过向 ConfigureServices 添加以下内容来添加依赖注入:

In Startup.cs, add the dependency injection by adding the following to ConfigureServices:

services.AddScoped, CustomUserStore>();
确保使用相同的标识对象,否则将无法工作!

services.AddScoped<IUserStore<IdentityUser>, CustomUserStore>();
Make sure to use the same Identity Object or it will not work!

现在,当您执行此操作时,如果在添加声明或角色之前将用户保存到数据库,UserManager 仍然无法完全工作.如果出现故障,您必须实现自己的数据回滚.例如,我的 AuthController 具有尝试添加角色的逻辑.如果失败,我将删除到刚刚创建的用户:

Now, when you do this, UserManager still does not fully work without saving the user to the database before adding claims or roles. You have to implement your own rollback of data if things fail. For instance, my AuthController has logic to try and add a role. If that fails, I will remove to user just created:

                var isCreated = await _userManager.CreateAsync(newUser, user.Password);
                await _context.SaveChangesAsync();  

                if (!isCreated.Succeeded)
                {
                    var roleResult = await _userManager.AddToRoleAsync(newUser, "User");

                    if (!roleResult.Succeeded)
                    {
                        throw new Exception("not saved");

                    }
                    await _context.SaveChangesAsync();
                    var jwtToken = await GenerateAuthResult(newUser, null);
                        
                    return Ok(jwtToken);  // or whatever response needed
                }
                else
                {
                    await _userManager.DeleteAsync(newUser);
                    await _context.SaveChangesAsync()
                    return BadRequest( // add response );
                }

旧帖子:.NET Core 2.1 中 UserManager 的 AutoSaveChanges

Identity 上发布的问题不是完全事务性的:https://github.com/dotnet/aspnetcore/issues/18407

Issue posted on Identity not being fully transactional: https://github.com/dotnet/aspnetcore/issues/18407

这篇关于.net 5 在启动时将 AutoSaveChanges 设置为 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆