ASP.NET核心Cookie身份验证

时间:2017-05-09 13:41:01

标签: c# asp.net authentication cookies asp.net-web-api

我已经阅读了很多教程,我这样做应该是正确的,但我收到了意想不到的结果。 我正在创建我的ClaimsIdentity / Principal :(注意在类库项目中而不是主WebApi项目.user = from database):

var claims = new List<Claim>()
{
    new Claim(ClaimTypes.Email, user.Email),
    new Claim(ClaimTypes.Name, user.FirstName)
};  
var identity = new ClaimsIdentity(claims, "Basic");
var prinipal = new ClaimsPrincipal(identity);

当我在此时调试时,我可以注意到Claims属性似乎是空的,但是有一个私有的m_instanceClaims字段具有正确的声明,这对我来说很奇怪我会期望设置ClaimsIdentity.Claims属性。

在我的控制器中,我这样登录:

await HttpContext.Authentication.SignInAsync("Custom.Identity", principal);

但是当我调试控制器的User属性时,标识不包含任何声明,名称和isAuthentecated设置为false。
但是,principal属性的isAuthenticated设置为true,因此SignIn方法本身存在问题?
所有后续请求还会将User的身份显示为没有任何声明,无论是空还是假。

在我的StartUp.cs中,我有:

...
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    AuthenticationScheme = "Custom.Identity",
    LoginPath = new PathString("/api/Account/Login"),
    AccessDeniedPath = new PathString("/api/Account/Denied"),
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
});
app.UseMvc();

这是非常简单的代码,我做错了什么?

0 个答案:

没有答案