Beispiel #1
0
        public async Task <IdentityResult> CreateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            Guid applicationId;
            var  tnx = await db.Database.BeginTransactionAsync(IsolationLevel.Serializable, cancellationToken);

            var application = await db.AspnetApplications.SingleOrDefaultAsync(c => c.ApplicationName == applicationName, cancellationToken);

            if (application == null)
            {
                applicationId = Guid.NewGuid();
                var newApplication = new AspnetApplications
                {
                    ApplicationId          = applicationId,
                    ApplicationName        = applicationName,
                    LoweredApplicationName = applicationName?.ToLowerInvariant(),
                };
                await db.AspnetApplications.AddAsync(newApplication, cancellationToken);
            }
            else
            {
                applicationId = application.ApplicationId;
            }
            Guid userId  = Guid.NewGuid();
            var  newUser = new AspnetUsers
            {
                ApplicationId    = applicationId,
                UserId           = userId,
                UserName         = user.UserName,
                LoweredUserName  = user.NormalizedUserName,
                IsAnonymous      = false,
                LastActivityDate = user.LastActivityDate,
            };
            await db.AspnetUsers.AddAsync(newUser, cancellationToken);

            var newMembership = new AspnetMembership
            {
                ApplicationId           = applicationId,
                UserId                  = userId,
                Password                = user.PasswordHash,
                PasswordSalt            = utility.GenerateSalt(),
                Email                   = user.Email,
                LoweredEmail            = user.Email?.ToLowerInvariant(),
                PasswordQuestion        = user.PasswordQuestion,
                IsApproved              = user.IsApproved,
                IsLockedOut             = user.IsLockedOut,
                CreateDate              = user.CreationDate,
                LastLoginDate           = user.LastLoginDate,
                LastPasswordChangedDate = user.LastPasswordChangedDate,
                LastLockoutDate         = user.LastLockoutDate,
            };
            await db.AspnetMembership.AddAsync(newMembership, cancellationToken);

            await db.SaveChangesAsync(cancellationToken);

            tnx.Commit();
            return(IdentityResult.Success);
        }
        public async Task <ActionResult <AspnetUsers> > PostAspnetUsers([FromBody] AspnetUsers aspnetUsers)
        {
            var aspNetApp = new AspnetApplications
            {
                ApplicationId          = Guid.NewGuid(),
                ApplicationName        = "newApplication" + DateTime.Now.ToString(),
                LoweredApplicationName = "new application" + DateTime.Now.ToString(),
                Description            = "testApplication"
            };

            aspnetUsers.Application = aspNetApp;
            _context.AspnetApplications.Add(aspNetApp);

            _context.AspnetUsers.Add(aspnetUsers);

            await _context.SaveChangesAsync();

            // return CreatedAtAction("GetAspnetUsers", new { id = aspnetUsers.UserId }, aspnetUsers);
            return(CreatedAtAction("GetAspnetUsers", new { id = aspnetUsers.UserId }, aspnetUsers.UserName));
        }