2017-04-19 106 views
1

我在一个ASP MVC登录表单上工作。为什么我的microsoft.owin.security.authenticationmanager登录方法不起作用?

我有非常简单的代码。 A Startup类和尝试设置Cookie的操作。下面是我的代码:

启动 它位于App_Start(也有与key="owin:AppStartup"对它的引用在<appSetting>

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "ApplicationCookie", 
      LoginPath = new PathString("/auth/login"), 
     }); 
    } 
} 

被假设验证用户是该动作的方法:

[HttpPost] 
public ActionResult Login(user model) 
{ 
    if(ModelState.IsValid) 
    { 
     var identity = new ClaimsIdentity(new[] 
     { 
      new Claim(ClaimTypes.Email, "[email protected]"), 
      new Claim(ClaimTypes.Name, "tom"), 
      new Claim(ClaimTypes.Role, "admin") 
     }); 

     var ctx = Request.GetOwinContext(); 
     var authManager = ctx.Authentication; 
     authManager.SignIn(identity); 
     return RedirectToAction("Index", "Home"); 
    } 
    return View(model); 
} 

但这没有得到身份认证为@User.Authenticated在我的_Layout.cshtml中是错误的,当return RedirectToAction("Index", "Home");和debbuger显示IsAuthenticated属性为false(在控制器Login动作中和在_Layout.cshtml中。

我已检查IIS是否为匿名身份验证使用我的Windows管理工具启用,还我已经检查了启动设置应用程序启动时...

我看来,authManager.SignIn(identity)没有做的工作。

我们该如何解决?

debugger screenshot

PS:我甚至不看到浏览器弹出,询问我是否保存密码

回答

1

签到仍然存在(我在我的测试,即使用户仍然没有通过身份验证弹出只有一次)用户未来的请求(通过cookie),它不会改变当前的请求。如果需要,您可以直接为当前请求设置HttpContext.User。

我还记得您需要将ClaimsIdentity AuthenticationType设置为CookieAuthenticationDefaults.AuthenticationType(或您用于识别中间件的任何身份验证类型)。否则cookie认证中间件将不会激活。

相关问题