我已经阅读了很多教程,我这样做应该是正确的,但我收到了意想不到的结果。 我正在创建我的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();
这是非常简单的代码,我做错了什么?