使用ASP.NET Identity UserManager进行事务

nmi*_*026 4 c# asp.net transactions asp.net-identity usermanager

我正在尝试更新用户.

AppUserManager appUserManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();

AppUser member = await appUserManager.FindByIdAsync(User.Identity.GetUserId());

member.HasScheduledChanges = true;

IdentityResult identityResult = appUserManager.Update(member);
Run Code Online (Sandbox Code Playgroud)

如果后续调用Web API失败,我需要回滚对用户的任何更改.我知道交易,像这样:

using (var context = HttpContext.GetOwinContext().Get<EFDbContext>())
 {
    using (var dbContextTransaction = context.Database.BeginTransaction())
    {      
        try
        {   
            // Changes

            member.HasScheduledChanges = true;

            // Would this be transactional?
            IdentityResult identityResult = appUserManager.Update(member);               

            context.SaveChanges();

            dbContextTransaction.Commit();
        }
        catch //(Exception ex)
        {

            // dbContextTransaction.Rollback(); no need to call this manually.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在try块中使用AppUserManager进行的操作是否是事务性的?另外,他们使用相同的EFDbContext实例吗?换句话说,我不知道第二个代码示例开头的var context是否会被try块中的appUserManager"Update"方法调用使用.

此外,AppUserManager的创建方式如下:

public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{           

    EFDbContext db = context.Get<EFDbContext>();

    AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));

    // etc.

    return manager;
}
Run Code Online (Sandbox Code Playgroud)

tra*_*max 7

EFDbContext在您的示例中是相同的 - 在两种情况下,您都可以从OWIN上下文中解析它们,因此这不是问题.但是,Identity以存储无关的方式编写,这意味着存储机制可以由非SQL Server替换.这要求内部缺乏交易AppUserManager.所以你需要创建自己的.

我经常var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)在我的生产应用程序中使用(只有更多的架构):

using(var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    try
    {
        AppUserManager appUserManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();

        AppUser member = await appUserManager.FindByIdAsync(User.Identity.GetUserId());

        member.HasScheduledChanges = true;

        IdentityResult identityResult = appUserManager.Update(member);
        scope.Complete();
    }
    catch (Exception ex)
    {
        scope.Dispose();
        throw;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • scope.Dispose() 在您的异常处理程序中是完全多余的。 (3认同)
  • 是的,您可以通过`dbContext`直接操作用户记录.这就是`AppUserManager`所做的,只是它增加了一点验证 (2认同)

adn*_*nan 5

使用asp.net身份UserManager进行事务提交/回滚的完整解决方案

var appDbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
using (var identitydbContextTransaction = appDbContext.Database.BeginTransaction())
{
   try
   {
       var result = await UserManager.CreateAsync(user, "password");
       if (result.Succeeded)
       {
         var userinfo = await UserManager.FindByNameAsync("Email");
         var userId = user.Id;
         await UserManager.AddToRoleAsync(userId, "rolename");

         identitydbContextTransaction.Commit();
       }
  }
  catch (Exception)
  {
        identitydbContextTransaction.Rollback();
  }
}
Run Code Online (Sandbox Code Playgroud)

它可能会帮助您,使用asp.net身份UserManager进行交易。但它对我有效,如果事务中发生任何错误,它将回滚所有事务。

  • 我可能是错的,但这里的回滚不是多余的吗?如果发生异常,事务将被处理并且更改将丢失。 (2认同)