I have a project for a virtual store. The store already exists today in ASP and I'm migrating to .NET. I have the intention of using the authentication control of the .net itself, however when I started the project, I started a blank (without authentication), and now I am trying to implement.
I have already found some tutorials on the internet that I tried to follow, but so far none have been able to make the solution work.
Let's go to what I have ready so far. I already installed in my project, as I found guidelines:
Microsoft.AspNet.Identity.EntityFramework
Microsoft.Owin.Security
Microsoft.Owin.Host.SystemWeb
I have already changed my model that I want to use to login to be 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 created 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 looks 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 to the line:
var user = await userManager.FindAsync(email, senha);
Because he does not even make a mistake and does not even go forward. The system simply hangs there.
After catching a little, I also tried to create the file identityConfig.cs
, and in class ApplicationUsarManager
, I had to use my own class (clients), but it also did not work:
public class ApplicationUserManager : UserManager<clientes>
{
public ApplicationUserManager(IUserStore<clientes> store)
: base(store)
{
}
}
Can anyone help me?
If you need more information, let me know.