views:

1149

answers:

2

Hi!

I've implemented some basic, custom membership provider for my ASP.NET MVC application so I thought that all validation will be done in my custom code.

Unfortunately when I'm trying to create new user by calling function:

Membership.CreateUser(user.UserName, user.Password, user.Email, null, null, true, Guid.NewGuid(), out status);

which should eventually throw an exception with all validation errors I'm getting a status like "InvalidUserName" or "InvalidPassword" instead... That means that my custom CreateUser function isn't call directly, it's used after some basic validation which I would wish to skip.

My CreateUser function (in my custom provider):

public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
    try
    {

        User user = new User();
        user.UserKey = Guid.NewGuid();
        user.UserName = username;
        user.passwordSalt = string.Empty;
        user.Password = this.TransformPassword(password, ref user.passwordSalt);
        user.Email = email;
        user.PasswordQuestion = passwordQuestion;
        user.PasswordAnswer = passwordAnswer;
        user.CreationDate = DateTime.Now;
        user.LastActivityDate = DateTime.Now;
        user.LastLoginDate = DateTime.MinValue;
        user.LastPasswordChangeDate = DateTime.Now;

        this._UsersRepository.SaveUser(user);

        status = MembershipCreateStatus.Success;
        return CreateMembershipFromInternalUser(user);


    }
    catch(RuleException ex)
    {
        throw ex;
    }
}

Do you know how to enforce direct usage of custom CreateUser function !?

A: 

Complete rewrite

The ASP.NET Membership system always does some initial validation of the inputs when APIs such as CreateUser are called.

I don't know of any way around this aside from not going directly to the ASP.NET membership APIs.

Eilon
Ok, thank you...
Kotu
A: 

But I'm not using a default ASP.NET MVC project's AccountController...

Just take a look:

[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Register(User user, string password_confirm, bool acceptsTerms)
{
    if (!acceptsTerms)
        ModelState.AddModelError("acceptsTerms", "Musisz zaakceptować regulamin");

    if (ModelState.IsValid)
    {
        try
        {
            MembershipCreateStatus status = new MembershipCreateStatus();
            Membership.CreateUser(user.UserName, user.Password, user.Email, null, null, true, Guid.NewGuid(), out status);
        }
        catch (RuleException ex){
            ex.CopyToModelState(ModelState,"user");
        }
    }

    return View();
}

The point is that I'm getting a status instead of RuleException ex when user.UserName or user.Password is empty. My custom RuleException ex would give me back such informations as well. Where a status value is assigned right now !? Bacouse it's not done in my implementation of CreateUser...

Kotu
How is your User object defined? Does it have Data Annotations validator attributes on it?
Eilon
Also, instead of posting a new answer, please edit your original question with more information.
Eilon
No I don't have any validator attributes there yet, It's just a class mapped to database table (LINQ).
Kotu
I'm still not sure I understand. What exactly is it that you're expecting? You're saying the model is valid and that you're getting a "status" which would seem to imply that Membership.CreateUser *is* getting called. Have you tried setting a breakpoint (put the cursor on the line and press F9) and then debugging the site (press F5 to run the site with the debugger)? Then you can press F10 to stop over each line of code and hover over variables with your mouse to see the values.
Eilon
Yes, I've been using debugger...I'm expecting that my version of CreateUser function will be used instantly, but it's not... I've pasted it to my first post.
Kotu
Where status is changed to InvalidUserName !? As you can see I'm setting it by default to "Success" (status = MembershipCreateStatus.Success;)
Kotu