What is the recommended way to run asp.net authentication features in a transaction?

Using identifier asp.net version of RTW .

I need to perform several actions in a transaction, including function calls UserManangerand other operations on mine DbContext(example: create a new user, add him to a group and perform some business logical operations).

How can I do it?

My thoughts follow.

Transactionaction

using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
    // Do what I need
    if (everythingIsOk) scope.Complete();
}

The problem is that the functions are UserManagerall asynchronous, and TransactionScopenot designed to work with async / await. It seems to be solved in .Net Framework 4.5.1 . But I use Azure Web Sites to host my projects, so I can’t configure it yet 4.5.1.

Database operation

public class SomeController : Controller
{
    private MyDbContext DbContext { get; set; }
    private UserManager<User> UserManager { get; set; }

    public AccountController()
    {
        DbContext = new MyDbContext()
        var userStore = new UserStore<IdentityUser>(DbContext);
        UserManager = new UserManager<IdentityUser>(userStore);
    }

    public async ActionResult SomeAction()
    {
        // UserManager uses the same db context, so they can share db transaction
        using (var tran = DbContext.Database.BeginTransaction())
        {
            try
            {
                // Do what I need
                if (everythingIsOk)
                    tran.Commit();
                else
                {
                    tran.Rollback();
                }
            }
            catch (Exception)
            {
                tran.Rollback();
            }
        }
    }
}

, ?

UserManager<> IUserStore<>, .

UserStore<> DbContext, , .

+4
1

, unit test.

EF UserStore , , DefaultConnection. DatabaseInitializer, / , db .

0

All Articles