コード例 #1
0
        //
        // MembershipProvider.CreateUser
        //
        public override MembershipUser CreateUser(string username,
                 string password,
                 string email,
                 string passwordQuestion,
                 string passwordAnswer,
                 bool isApproved,
                 object providerUserKey,
                 out MembershipCreateStatus status)
        {
            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            MembershipUser u = GetUser(username, false);

            if (u == null)
            {
                DateTime createDate = DateTime.Now;
                if (providerUserKey == null)
                {
                    providerUserKey = Guid.NewGuid();
                }
                else
                {
                    if (!(providerUserKey is Guid))
                    {
                        status = MembershipCreateStatus.InvalidProviderUserKey;
                        return null;
                    }
                }

                User user = new User()
                    {
                        Email = email,
                  //      ProviderUserKey = providerUserKey,
                        Username = username,
                        Password = EncodePassword(password),
                        PasswordQuestion = passwordQuestion,
                        PasswordAnswer = EncodePassword(passwordAnswer),
                        IsApproved = isApproved,
                        Comment = "",
                        CreationDate = createDate,
                        LastPasswordChangedDate = createDate,
                        LastActivityDate = createDate,
                        ApplicationName = pApplicationName,
                        IsLockedOut = false,
                        LastLockedOutDate = createDate,
                        FailedPasswordAttemptCount = 0,
                        FailedPasswordAttemptWindowStart = createDate,
                        FailedPasswordAnswerAttemptCount = 0,
                        FailedPasswordAnswerAttemptWindowStart = createDate
                    };

                try
                {
                    _neoClient.Cypher
                                .Create("(user:User {newUser})")
                                .WithParam("newUser", user)
                                .ExecuteWithoutResults();
                    Node<User> newUser = _neoClient.Cypher
                             .Match("(n:User)")
                             .Where((User n) => (n.Username == username && n.ApplicationName == pApplicationName))
                             .Return<Node<User>>("n").Results.FirstOrDefault();

                    if (newUser != null)
                    {
                        // ProviderUserKey must be an int for WebSecurity to work right...
                        // http://aspnetwebstack.codeplex.com/SourceControl/latest#src/WebMatrix.WebData/WebSecurity.cs
                        _neoClient.Update<User>(newUser.Reference, un =>
                        {
                            un.ProviderUserKey = newUser.Reference.Id;
                        });
                        newUser.Data.ProviderUserKey = (int)newUser.Reference.Id;
                        status = MembershipCreateStatus.Success;
                    }
                    else
                    {
                        status = MembershipCreateStatus.ProviderError;
                    }
                }
                catch (Exception e)
                {
                    if (WriteExceptionsToEventLog)
                    {
                        WriteToEventLog(e, "CreateUser");
                    }

                    status = MembershipCreateStatus.ProviderError;
                }
                return GetUser(username, false);
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
            }
            return null;
        }
コード例 #2
0
        public override string CreateAccount(string userName, string password, bool requireConfirmationToken)
        {
            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(userName, password, true);
            OnValidatingPassword(args);

            if (args.Cancel)
            {
                return null;
            }

            MembershipUser u = GetUser(userName, false);

            if (u == null)
            {
                DateTime createDate = DateTime.Now;
                Guid provider = Guid.NewGuid();
                Guid confirmToke= Guid.NewGuid();

                try
                {
                    User user = new User()
                    {
                        Email = userName,
                        // This must be an Int for WebSecurity GetUserId to work
                        // http://aspnetwebstack.codeplex.com/SourceControl/latest#src/WebMatrix.WebData/WebSecurity.cs
                    //    ProviderUserKey = provider,
                        Username = userName,
                        Password = EncodePassword(password),
                        Comment = "",
                        CreationDate = createDate,
                        LastPasswordChangedDate = createDate,
                        LastActivityDate = createDate,
                        ApplicationName = pApplicationName,
                        IsLockedOut = false,
                        IsApproved = true,
                        LastLockedOutDate = createDate,
                        FailedPasswordAttemptCount = 0,
                        FailedPasswordAttemptWindowStart = createDate,
                        FailedPasswordAnswerAttemptCount = 0,
                        FailedPasswordAnswerAttemptWindowStart = createDate,
                        AccountConfirmToken = confirmToke.ToString(),
                        IsConfirmed = requireConfirmationToken
                    };

                    _neoClient.Cypher
                                .Create("(user:User {newUser})")
                                .WithParam("newUser", user)
                                .ExecuteWithoutResults();
                    Node<User> newUser = _neoClient.Cypher
                             .Match("(n:User)")
                             .Where((User n) => (n.Username == userName && n.ApplicationName == pApplicationName))
                             .Return<Node<User>>("n").Results.FirstOrDefault();

                    if (newUser != null)
                    {
                        // we need to update the provider token...
                        _neoClient.Update<User>(newUser.Reference, un =>
                        {
                            un.ProviderUserKey = newUser.Reference.Id;
                        });
                        return newUser.Data.AccountConfirmToken;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception e)
                {
                    if (WriteExceptionsToEventLog)
                    {
                        WriteToEventLog(e, "CreateAccount");
                    }

                }
            }
            return null;
        }