Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using WebSercurity.CreateAccount with other queries in a TrasactionScope, without DTC Enabled

Problem. In a registration scenario, I'm trying to insert a user in my User table and then call WebSercurity.CreateAccount for that user (in a transaction). This causes the error that MS DTC is not available on the server.

Description. The reason I'm doing this is because I have a Customer Entity which inherits from User, so WebSercurity.CreateUserAndAccount cannot be used because it doesn't know about Customer and just inserts a User record.

I'm using Asp.net MVC 4 with EntityFramework 5, CodeFirst, and SQL Server 2008 R2.

any suggestions for not using DTC would be appreciated!

EDIT. It is obvious why this error occurs, because websecurity uses its own connection to the database, and my repositories use another connection, although I've configured simplemembership to use the same DbContext class as my repositories, but the problem is it creates a new instance of the DbContext ...

I was hoping if there is a way to pass an existing context object, or connection to the WebSecurity to use with its methods.

here's the code:

        if (ModelState.IsValid)
        {
            //using (TransactionScope tx = new TransactionScope())
            //{
                UnitOfWork.UserRepository.Insert(new Customer
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Tel = model.Tel,
                    Mobile = model.Mobile,
                    BirthDate = model.BirthDate,
                    InsertDate = DateTime.Now,
                    UserType = UserType.Customer,
                    MaritalStatus = model.MaritalStatus,
                    ZipCode = model.ZipCode,
                    StreetAddress = model.StreetAddress,
                    City = model.City,
                    State = model.State
                });
                UnitOfWork.Commit();

                string token = WebSecurity.CreateAccount(model.Email, model.Password, true);

                Roles.AddUserToRole(model.Email, "Customer");
                //WebSecurity.Login(model.Email, model.Password, true);

                await Task.Run(() => EmailHelper.SendConfrimationEmail(token, model.Email));

            //  tx.Complete();
            //}
like image 281
Ashkan Avatar asked Nov 03 '22 04:11

Ashkan


1 Answers

The DTC error occurs because you are trying to span a transaction over two different database connections. You have several options.

SimpleMembership is designed for simple scenarios. You are doing an advanced scenario, so you should probably use a different membership provider.

like image 129
Erik Funkenbusch Avatar answered Nov 15 '22 13:11

Erik Funkenbusch