Identity implementation in existing project - Not working

Asked

Viewed 924 times

0

I have a project for a virtual store. The store already exists today made in ASP and I’m migrating to .net. I intend to use the authentication control itself . net, but when I started the project, I started a blank (no authentication), and now I’m trying to implement.

I have found some tutorials on the internet that I tried to follow, but so far none managed to make the solution work.

Let’s go for what I have ready so far. I have installed in my project, as I found guidelines:

Microsoft.AspNet.Identity.EntityFramework
Microsoft.Owin.Security
Microsoft.Owin.Host.SystemWeb

I already changed my model that I want to use to login to be the IdentityUser:

public partial class clientes : IdentityUser
{
    public decimal ID_CLIENTE { get; set; }
    public string NOME { get; set; }
    public string EMAIL { get; set; }
...
}

I’ve already started my startup:

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
using EmocioneJa.Models;
using System;
using Microsoft.AspNet.Identity.EntityFramework;

[assembly: OwinStartup(typeof(EmocioneJa.Repositories.Startup))]

namespace EmocioneJa.Repositories
{
public class Startup
{
    public static Func<UserManager<clientes>> UserManagerFactory { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Cadastro/Login")
        });

        UserManagerFactory = () =>
        {
            var usermanager = new UserManager<clientes>(new UserStore<clientes>(new emocionejaEntities()));
            // allow alphanumeric characters in username
            usermanager.UserValidator = new UserValidator<clientes>(usermanager)
            {
                AllowOnlyAlphanumericUserNames = false
            };

            return usermanager;
        };
    }
}
}

My Controller is like this:

[AllowAnonymous]
public class CadastroController : Controller
{
    private readonly UserManager<clientes> userManager;

    emocionejaEntities _db;

    public CadastroController()
        : this(Startup.UserManagerFactory.Invoke())
    {
        _db = new emocionejaEntities();
    }


    public CadastroController(UserManager<clientes> userManager)
    {
        this.userManager = userManager;
    }

public async Task<ActionResult> FazerLogin(string email, string senha)
    {
        funcoes fc = new funcoes();
        string _nextPage = "";

        senha = funcoes.Base64Encode(senha);

        var user = await userManager.FindAsync(email, senha);

        if(user != null)
        {
            var identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            identity.AddClaim(new System.Security.Claims.Claim("ID_CLIENTE", user.ID_CLIENTE.ToString()));

HttpContext.Request.GetOwinContext().Authentication.SignIn(identity);
        }
    }
}

The problem is when you get on the line:

var user = await userManager.FindAsync(email, senha);

Because it doesn’t even make a mistake and it doesn’t go forward. The system just hangs there.

After catching a little, I also tried to create the file identityConfig.cs, and in class ApplicationUsarManager, I ordered to use my own class (clients), but also did not help:

public class ApplicationUserManager : UserManager<clientes>
{
    public ApplicationUserManager(IUserStore<clientes> store)
        : base(store)
    {
    }
}

Someone can help me?

If you need more information, let me know.

1 answer

-2

So buddy, you’re using a custom class to work with Usermanager. You must implement the Userstore class to work with your custom entity/table, in your case the client class.

That one link explains well how to implement...

  • 1

    Hello Bruno, the link you provided may not be available in the future, compromising your response. Transport it here in a way that meets the problem proposed by the author of the question. Even it is in English.

  • @Ismael’s comment is totally meaningless!

Browser other questions tagged

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