Customize Asp.net Identity - Multiple classes like User in Identity

3

We know that our user in asp.net identity is the class with name of ApplicationUser I would like to create other classes that inherit from it Because? Why, let's say I have the Client, Vendor, User class.

I want them all to be users of my system

So I have my default class that comes with creating the project:

public class ApplicationUser : IdentityUser
{
}

And I have my client class for example.

public class Cliente: ApplicationUser
{
    public string CNPJ { get; set; }
}

When going to the Register action, I passed the Client to register as user, since it inherits from ApplicationUser:

public async Task<ActionResult> Register(Cliente model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser() { 
            UserName = model.UserName,
        };
        var result = await UserManager.CreateAsync(user, model.PasswordHash);
        if (result.Succeeded)
        {
            await SignInAsync(user, isPersistent: false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            AddErrors(result);
        }
    }
    return View(model);
}

When going to this Action, I encounter the following error:

Invalid column name 'CNPJ'.

When trying to call method CreateAsync(user,model.PasswordHash)

What am I doing wrong? Can I do what I'm doing? Inherit and have multiple classes as "user" in identity?

PS: I've done migrations and gave update-database

    
asked by anonymous 22.05.2014 / 00:00

1 answer

2

Two ways to solve:

1. Make Client inherit direct from IdentityUser

public class Cliente : IdentityUser
{
     [CNPJ]
     public string CNPJ { get; set; }
}

2. Place CNPJ directly into ApplicationUser

public class ApplicationUser : IdentityUser
{
     [CNPJ]
     public string CNPJ { get; set; }
}

Taking advantage of it, I'll give you my CNPJAttribute for validation:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class CNPJ : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null) return null;

        int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
        int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };

        int soma, resto;

        string digito, tempCnpj, CNPJ;

        CNPJ = value.ToString().Trim();
        CNPJ = CNPJ.Replace(".", "").Replace("-", "").Replace("/", "");

        if (CNPJ.Length != 14)
            return new ValidationResult("CNPJ Inválido.");

        tempCnpj = CNPJ.Substring(0, 12);
        soma = 0;

        for (int i = 0; i < 12; i++)
            soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];
        resto = (soma % 11);

        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = resto.ToString();
        tempCnpj = tempCnpj + digito;
        soma = 0;

        for (int i = 0; i < 13; i++)
            soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];
        resto = (soma % 11);

        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = digito + resto.ToString();

        if (CNPJ.EndsWith(digito))
            return null;
        else
            return new ValidationResult("CNPJ Inválido.");
    }

    public override string FormatErrorMessage(string name)
    {
        return name;
    }
}
    
22.05.2014 / 00:24