Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - How to create transaction with asp.net identity?

I am doing registration on which i am asking for 5 things:

FullName,EmailId,Password,ContactNumber,Gender

Now emailid and password i am storing with register method and given in below two link:

public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

            using var context = new MyEntities())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var DataModel = new UserMaster();
                        DataModel.Gender = model.Gender.ToString();
                        DataModel.Name = string.Empty;
                        var result = await UserManager.CreateAsync(user, model.Password);//Doing entry in AspnetUser even if transaction fails
                        if (result.Succeeded)
                        {
                            await this.UserManager.AddToRoleAsync(user.Id, model.Role.ToString());
                            this.AddUser(DataModel, context);
                            transaction.Commit();
                            return View("DisplayEmail");
                        }
                        AddErrors(result);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return null;
                    }

                }
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

public int AddUser(UserMaster _addUser, MyEntities _context)
    {
        _context.UserMaster.Add(_addUser);           
        _context.SaveChanges();
        return 0;
    }

Now with this below 2 lines:

var result = await UserManager.CreateAsync(user, model.Password);//entry is done in AspnetUsers table.
await this.UserManager.AddToRoleAsync(user.Id, model.Role.ToString());//entry is done is Aspnetuserrole table

Now this Fullname,contactno,gender i am having in another table that is UserMaster.

So when i will submit my registration form i will save this details in UserMaster and AspnetUsers,AspnetUserinrole table.

But consider if there any problem occurs while saving record in UserMaster then i dont want to save entry in Aspnetuser and Aspnetuserinrole too.

I would like to create a transaction where i would rollback if any problem occurs during saving any record in any table i.e no entry should be done in AspnetUser,AspnetUserinRole nd userMaster.

Records should be saved successfully only if there is no problem in saving record in this 3 tables otherwise whiole transaction should be role back.

I am using Microsoft.AspNet.Identity for login,Register,role management and other and following this tutorial:

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity

But as await UserManager.CreateAsync and UserManager.AddToRoleAsync method are built in method how would i synchonize it to work with entity framework.

So can anybody guide me how to create such transaction or anything that would solve this?

IdentityConfig:

public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = 
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }

    // Configure the application sign-in manager which is used in this application.
    public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        {
        }

        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
        }

        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not create a new db context, but use the existing one.

var context = Request.GetOwinContext().Get<MyEntities>()

It is created per request if you use default implementation.

app.CreatePerOwinContext(ApplicationDbContext.Create);

Update:

OK, since you are using two different contexts your code will look something like this:

public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

        var appDbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
        using( var context = new MyEntities())
        using (var transaction = appDbContext.Database.BeginTransaction())
        {
            try
            {
                var DataModel = new UserMaster();
                DataModel.Gender = model.Gender.ToString();
                DataModel.Name = string.Empty;

                // Doing entry in AspnetUser even if transaction fails
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await this.UserManager.AddToRoleAsync(user.Id, model.Role.ToString());
                    this.AddUser(DataModel, context);
                    transaction.Commit();
                    return View("DisplayEmail");
                }
                AddErrors(result);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                return null;
            }
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

public int AddUser(UserMaster _addUser, MyEntities _context)
{
    _context.UserMaster.Add(_addUser);
    _context.SaveChanges();
    return 0;
}

Here, appDbContext is the same context that is used by UserManager.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...