Using WebSercurity.CreateAccount with other requests in TrasactionScope without DTC Enabled

Problem. In the login script, I try to insert a user into my User table and then call WebSercurity.CreateAccountfor that user (in a transaction). This leads to an error that MS DTC is not available on the server.

Description. The reason I do this is because I have an object Customerthat inherits from User, so WebSercurity.CreateUserAndAccountyou cannot use it because it does not know about Customerand just inserts the record User.

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

Any suggestions for using DTC will be appreciated!

EDIT. Obviously, why this error occurs because websecurity uses its own database connection and my repositories use a different connection, although I configured simplemembership to use the same class DbContextas my repositories, but the problem is that it creates a new one instance DbContext...

I was hoping if there is a way to pass an existing context object or join with WebSecurityfor use with its methods.

here is 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();
            //}
+5
source share
3 answers

, , . , , simplemembership POCO ( WebSecurity).

, , CreateAccount , webpages_Membership AddUserToRole, webpages_UsersInRoles. , GetUser ... WebSecurity Roles, .

, ( - ), , !

, - , .

+1

DTC - , . .

SimpleMembership . , , , .

+1

Mystere Man, , . . , .

, SimpleMembership, , . . , , SimpleMembership, .

You can also associate information in your other database with a unique user identifier (int) that uses SimpleMembership. After creating a user and registering him, you can get this identifier by calling WebSecurity.CurrentUserId .

You can go around the entire SimpleMempership provider and create your own membership provider, which is discussed here .

0
source

All Articles