Asp.net 带有owin cookie中间件的MVC 5-owinContext.Authentication.sign-in不发布cookie
我正在使用MVC5.2,并试图让Owin cookie中间件正常工作 在我的登录控制器中,我执行以下操作:Asp.net 带有owin cookie中间件的MVC 5-owinContext.Authentication.sign-in不发布cookie,asp.net,asp.net-mvc,cookies,forms-authentication,owin,Asp.net,Asp.net Mvc,Cookies,Forms Authentication,Owin,我正在使用MVC5.2,并试图让Owin cookie中间件正常工作 在我的登录控制器中,我执行以下操作: public class LoginController { [AllowAnonymous] public ActionResult Login(LoginViewModel loginViewModel) { //authenticate .... var claims = new List<Claim>
public class LoginController
{
[AllowAnonymous]
public ActionResult Login(LoginViewModel loginViewModel)
{
//authenticate
....
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "abc"),
new Claim(ClaimTypes.Email, "abc@abc.com")
};
var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
//create the cookie - i thought
authenticationManager.SignIn(new AuthenticationProperties{IsPersistent = true}, id);
//redirect to protected action
return RedirectToAction("Index", "RoutingController");
}
[Authorize]
public class RoutingController : Controller
{
[HttpGet]
public ActionResult Index()
{
return this.View();
}
}
下面是发生的情况
谢谢谢谢。调用用户管理器的行-我的用户将不在自定义数据库中,并且我可能不会使用实体框架来读取它们。在这种情况下它是如何工作的?@jonho这需要大量的工作,因为它们应该一起使用。您必须自己实现以下内容。IUserStore、IUserPasswordStore、iUserTwoFactoryStore、IUserClaimStore、IRoleStore、IUserSecurityStampStore、IUserRoleStore、UserManager如果没有这一行,就没有其他方法发布cookie了?感谢我目前正在使用SessionAuthenticationModule发布cookie,但这将我与IIS联系在一起。是否有其他方法来发布Owin cookie。你必须要有EF依赖项,这似乎很奇怪?感谢
HttpContext.GetOwinContext().Authentication.sign(新的AuthenticationProperties{IsPersistent=false},ident)代码>
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login/Login"),
CookieSecure = CookieSecureOption.Always
});
}
//
// POST: /Account/Login
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
@Html.AntiForgeryToken()