Asp.net mvc 5 使用新的OWIN标识API登录

Asp.net mvc 5 使用新的OWIN标识API登录,asp.net-mvc-5,owin,Asp.net Mvc 5,Owin,假设我有一个随机接受用户的网站。规则很简单;随机生成器可以告诉用户是否可以登录 那么我应该如何使用这个代码呢 private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.Create

假设我有一个随机接受用户的网站。规则很简单;随机生成器可以告诉用户是否可以登录

那么我应该如何使用这个代码呢

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    HttpContext.GetOwinContext().SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
我没有ApplicationUser,我没有注入DbContext,我没有users(!)表

主要问题;我的简单身份验证在哪里

  FormsAuthentication.RedirectFromLoginPage ("I_am_a_User", Persist.Checked)

代码看起来很难看,但我解决了

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

        List<Claim> _claims = new List<Claim>()
                                {
                                    new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
                                        Guid.NewGuid().ToString(), 
                                        "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"),

                                    new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
                                        "I_am_a_User", 
                                        "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"),

                                    new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
                                        "ASP.NET Identity", 
                                        "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"),                                        
                                };
        ClaimsIdentity identity = new ClaimsIdentity(_claims, DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
列表_索赔=新列表()
{
新索赔(”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
Guid.NewGuid().ToString(),
"http://www.w3.org/2001/XMLSchema#string“,“地方当局”),
新索赔(”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
“我是用户”,
"http://www.w3.org/2001/XMLSchema#string“,“地方当局”),
新索赔(”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
“ASP.NET标识”,
"http://www.w3.org/2001/XMLSchema#string“,“地方当局”),
};
ClaimsIdentity identity=新的ClaimsIdentity(_claims,DefaultAuthenticationTypes.applicationcokie);
SignIn(新的AuthenticationProperties(){IsPersistent=IsPersistent},标识);