Customizing Asp.net Identity - Multiple classes as User in Identity

Asked

Viewed 1,289 times

4

We know that our user on Asp.net Identity is the class named after ApplicationUser I would like to create other classes that inherit from her Why? Why, say I have the Customer, Seller, User class.

I want them all to be users of my system

So I have my default class that comes when 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 a 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 I go to Action, I find the following mistake:

Invalid column name 'CNPJ'.

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

What am I doing wrong? Can I do what I’m doing? Inherit and have multiple classes like "user" in Identity?

PS: I’ve done Migrations and I’ve updated database

1 answer

3


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 on Applicationuser

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

Enjoy, I’ll pass you mine 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;
    }
}
  • I have never tried something like this with so many layers of inheritance. I usually leave my applications at simplified layering levels. Suddenly I can try something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.