Question

I am building a site using ASP.net MVC 4 and Entity Framework 5.

I am using the SimpleMembership as a membership provider.

I have noticed recently that there are a number of duplicate usernames in the UserProfile table. For instance

UserID Username
100 myuser
101 myuser
134 myuser
187 myuser

My site still works as expected as the duplicate entries in the UserPfofile do not map not any records in the webpages_Membership table (so there is still effectively only 1 user called 'myuser')

However, I cannot work out how these duplicates are being created.

Any help would be appreciated

Was it helpful?

Solution

In your model use CustomValidationAttribute

AS suppose you have model as below than

 public class RegisterModel
 {

    [CustomValidation(typeof(RegisterModel), "CheckUserNameExists")]
    [Required(ErrorMessage="Required")]       
    public string UserName { get; set; }
}

Now create static method as below

 public static ValidationResult CheckUserNameExists(string userName)
    {

        User user = SessionUserNameEntity;

        if (user != null && user.UserName == userName)
        {
            return ValidationResult.Success;
        }
        else
        {
            if (userName != null)
            {
                User chkUser = new User();
                chkUser = FetchSingleUserFromExistDB(userName);
                if (chkUser != null)
                {
                    return new ValidationResult(Global.UserNameAlreadyRegistered);
                }
                else
                {
                    return ValidationResult.Success;
                }
            }
            else
            {
                return null;
            }
        }
    }

So whenever new User try to Register you can check that UserName is already is exist or not?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top