简体   繁体   中英

ASP.Net MVC simple Identity Login

I'm trying to learn Identity in ASP.Net. I created a very simple application, but I am having issues to set the test user to the CreateIdentity.

I created an OwinStartup:

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security.Cookies;

[assembly: OwinStartup(typeof(Identity.Startup))]

namespace Identity
{
    public class Startup
    {
        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("/Login")
            });
        }
    }
}

I have a simple page:

@using (Html.BeginForm("SignIn", "Login"))
{
    <input type="text" name="user" placeholder="user" />
    <input type="password" name="password" placeholder="Password" />
    <input type="submit" value="Login" id="btnSubmit" />
} 

Then, in my controller, I try to validate the user and password (hard code for my tests).

public ActionResult SignIn(UserModel model)
{
    // just for test
    string defaultPassword = "123456";
    string defaultUser = "test";

    if(model.User == defaultUser && model.Password == defaultPassword)
    {
        // Set authentication
        var userStore = new UserStore<IdentityUser>();
        var manager = new UserManager<IdentityUser>(userStore);
        var authenticationManager = HttpContext.GetOwinContext().Authentication;
        var userIdentity = manager.CreateIdentity(model, DefaultAuthenticationTypes.ApplicationCookie);
        authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
    }

    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

My problem is that manager.CreateIdentity is waiting for a variable type IdentityUser, not my model. How can I create a IdentityUser?

Thanks

Use Create method, then map the Identity model to your model:

    var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
    ApplicationUserManager _userManager = new ApplicationUserManager(store);
    var manager = _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var appUser = new ApplicationUser() { UserName = user.UserEmail, PhoneNumber = user.UserPhone, Email = user.UserEmail, PasswordHash = manager.PasswordHasher.HashPassword(user.UserPassword) };

    var createResult = manager.Create(appUser, appUser.PasswordHash);
    if (createResult.Succeeded)
    {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM