Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NETMVC简单身份登录_C#_Asp.net Mvc_Asp.net Identity - Fatal编程技术网

C# ASP.NETMVC简单身份登录

C# ASP.NETMVC简单身份登录,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,我正在尝试在ASP.Net中学习身份。我创建了一个非常简单的应用程序,但在将测试用户设置为CreateIdentity时遇到了问题 我创建了一个OwinStartup: using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; using Microsoft.AspNet.Identity; using Microsoft.Owin.Security.Cookies; [assembly: Ow

我正在尝试在ASP.Net中学习身份。我创建了一个非常简单的应用程序,但在将测试用户设置为CreateIdentity时遇到了问题

我创建了一个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")
            });
        }
    }
}
我有一个简单的页面:

@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" />
} 
@使用(Html.BeginForm(“登录”))
{
} 
然后,在我的控制器中,我尝试验证用户和密码(用于测试的硬代码)

public ActionResult登录(UserModel)
{
//只是为了测试
字符串defaultPassword=“123456”;
字符串defaultUser=“test”;
if(model.User==defaultUser&&model.Password==defaultPassword)
{
//设置身份验证
var userStore=new userStore();
var-manager=新的UserManager(userStore);
var authenticationManager=HttpContext.GetOwinContext().Authentication;
var userIdentity=manager.CreateIdentity(model,DefaultAuthenticationTypes.ApplicationOkie);
SignIn(新的AuthenticationProperties(){IsPersistent=false},userIdentity);
}
返回新的HttpStatusCodeResult(HttpStatusCode.OK);
}
我的问题是
manager.CreateIdentity
正在等待变量类型IdentityUser,而不是我的模型。如何创建标识用户


谢谢

使用创建方法,然后将标识模型映射到您的模型:

    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)
    {

    }
var store=new UserStore(new ApplicationDbContext());
ApplicationUserManager\u userManager=新的ApplicationUserManager(商店);
变量管理器=\u用户管理器??HttpContext.GetOwinContext().GetUserManager();
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.successed)
{
}

我是否应该创建一个IdentityUser变量类型并设置该变量的用户和密码?您可能需要查看以下网址的《入门指南》
    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)
    {

    }