开发者

Hashed Password with EF 4.1 Database first / ASP.NET MVC 3

I'm rolling my own authentication system and I've run into a situation that I need guidance on.

I'm not using EF 4.1 code first. I've designed my database to include a User table with an Id, Email and HashedPassword column. I then let EF create an entity based off this table. I then created a partial class to handle my validation.

The problem is that within my view I want to have a textbox for email, password and confirm password. I need validation on password and confirm password (required and compare). I can add these to the validation class I created but because they aren't part of the entity EF created for the User table, I get compiler errors when I try to view the page.

My partial class

[MetadataType(typeof (UserMetadata))]
    public partial class User
    {
        private class UserMetadata
        {
            [Required]
            public string Email { get; set; }

            [Required]
            public string Password { get; set; }

            [Required]
            [Compare("Password")]
            public string ConfirmPassword { get; set; }
        }
    }

My view

<h1>Sign Up</h1>
@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
    @Html.ValidationSummary()
    <p>
        @Html.LabelFor(x => x.Email)<br />
        @Html.TextBoxFor(x => x.Email)
    </p>
     <p>
        @Html.LabelFor(x => x.Password)<br />
        @Html.TextBoxFor(x => x.Password)
    </p>
    <p>
        开发者_C百科@Html.LabelFor(x => x.ConfirmPassword)<br />
        @Html.TextBoxFor(x => x.ConfirmPassword)
    </p>

    <p class="button">
        <input type="submit" value="Submit" /></p>
}

My model

Table Name: Users Columns: Id, Email, HashedPassword

How can I get around this?

Thanks


The most straightforward answer seems to be to make a ViewModel that has the Password and ConfirmPassword fields for the MVC part. When you're actually ready to save to the database, use that to create the EF model version with HashedPassword instead.

That said, I've used properties that don't exist in the EF model in my views without a ViewModel by adding them with a partial class as you're trying. I think the issue is that you have these inside the Metadata class, when they're not really metadata. Try this instead:

[MetadataType(typeof (UserMetadata))]
public partial class User
{
    [Required]
    public string Password { get; set; }

    [Required]
    [Compare("Password")]
    public string ConfirmPassword { get; set; }

    private class UserMetadata
    {
        [Required]
        public string Email { get; set; }


    }
}

This version defines a couple new properties, and then changes the metadata on Email to add [Required].


Your view's model is the User entity itself, which doesn't define a ConfirmPassword property. I would recommend that you create an entirely new view-model class, than you can use AutoMapper to easily map between these two classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜