Esempio n. 1
0
        public int CheckSchemaVersion(string feature)
        {
            const int version = 1;

            var db = new ProviderDataContext();
            var exists = db.aspnet_SchemaVersions.Any(p => p.Feature == feature.ToLower() & p.CompatibleSchemaVersion == version.ToString());
            var iStatus = exists ? 0 : 1;

            if (iStatus != 0)
            {
                throw new ProviderException(
                    StringResources.GetString(StringResources.ProviderSchemaVersionNotMatch,
                                                Provider.ToString(), version.ToString()));
            }
            return iStatus;
        }
        public CreateUserViewModel CreateUser(string salt, string pass, string encodedPasswordAnswer, string username, string email, string passwordQuestion, object providerUserKey, bool isApproved, bool requiresUniqueEmail, MembershipPasswordFormat passwordFormat)
        {
            var userId = (Guid?)providerUserKey;
            var errorCode = 0;
            bool newUserCreated = false;
            DateTime createDate = DataProviderHelper.RoundToSeconds(DateTime.UtcNow);          
            
            Guid? applicationId;
            CreateApplication(ApplicationName, out applicationId);
            var db = new ProviderDataContext();
            aspnet_User aspnetUser =
                db.aspnet_Users.SingleOrDefault(p => p.LoweredUserName == username && p.ApplicationId == applicationId);
            var newUserId = aspnetUser == null ? (Guid?)null : aspnetUser.UserId;
            if (!newUserId.HasValue)
            {
                try
                {
                    newUserId = aspnet_Users_CreateUser(applicationId, username, false, createDate, newUserId);
                }
                catch (Exception)
                {
                    errorCode = 10;
                }
                newUserCreated = true;
            }
            else
            {
                if (newUserId != userId & userId.HasValue)
                    return new CreateUserViewModel {Status = 6};
            }

            bool membershipExists = db.aspnet_Memberships.Any(p => p.UserId == newUserId);
            if (membershipExists)
                return new CreateUserViewModel { Status = 6 };

            if (requiresUniqueEmail)
            {
                membershipExists =
                    db.aspnet_Memberships.Any(p => p.ApplicationId == applicationId & p.LoweredEmail == email.ToLower());
                if (membershipExists)
                    return new CreateUserViewModel { Status = 7 };
            }

            if (!newUserCreated)
            {
                aspnet_User user = db.aspnet_Users.SingleOrDefault(p => p.UserId == userId);
                user.LastActivityDate = createDate;
            }

            var membership = new aspnet_Membership
            {
                ApplicationId = applicationId.Value,
                UserId = userId.Value,
                Password = pass,
                PasswordSalt = salt,
                Email = email,
                LoweredEmail = email.ToLower(),
                PasswordQuestion = passwordQuestion,
                PasswordAnswer = encodedPasswordAnswer,
                IsApproved = isApproved,
                IsLockedOut = false,
                CreateDate = createDate,
                LastLoginDate = createDate,
                LastPasswordChangedDate = createDate,
                LastLockoutDate = new DateTime(1754,1,1),
                FailedPasswordAttemptCount = 0,
                FailedPasswordAttemptWindowStart = new DateTime(1754,1,1),
                FailedPasswordAnswerAttemptCount = 0,
                FailedPasswordAnswerAttemptWindowStart = new DateTime(1754,1,1)
            };

            db.aspnet_Memberships.InsertOnSubmit(membership);
            db.SubmitChanges();


            if (errorCode < 0 || errorCode > (int)MembershipCreateStatus.ProviderError)
                errorCode = (int)MembershipCreateStatus.ProviderError;

            return new CreateUserViewModel
            {
                Status = errorCode,
                UserId = userId.Value,
                Date = createDate
            };
        }
        private static Guid? aspnet_Users_CreateUser(Guid? applicationId, string username, bool isAnonymous, DateTime lastActivityDate, Guid? userId)
        {
            var db = new ProviderDataContext();
            if (!userId.HasValue)
                userId = Guid.NewGuid();
            else
            {
                if (db.aspnet_Users.Any(p => p.UserId == userId))
                    throw new ProviderException("There is no user with userid " + userId);
            }

            var user = new aspnet_User
                           {
                                       ApplicationId = applicationId.Value,
                                       UserId = userId.Value,
                                       UserName = username,
                                       LoweredUserName = username.ToLower(),
                                       IsAnonymous = isAnonymous,
                                       LastActivityDate = lastActivityDate
                                   };
            db.aspnet_Users.InsertOnSubmit(user);
            db.SubmitChanges();
            return userId;
        }