完成登录代码后
var identity = new ClaimsIdentity(claims, OAuthConfigur.AuthenticationType);
this.AuthenticationManager.SignIn(new AuthenticationProperties
{
ExpiresUtc = DateTimeOffset.Now.AddMinutes(30),
IsPersistent = false
}, identity);
return RedirectToAction("Index", "Home");
在RedirectToAction之后,broswer中有cookie
但是当授权属性没有授权时。
在我的自定义Authorize actionfilter中,
httpContext.User.Identity.IsAuthenticated
总是返回false。
我找到了一种获取身份的方法:
private ClaimsIdentity GetIdentity(HttpContextBase httpContext)
{
var ticket = httpContext.GetOwinContext().Authentication
.AuthenticateAsync(OAuthConfigur.AuthenticationType).Result;
var identity = ticket != null ? ticket.Identity : null;
return identity;
}
在此功能之后,我可以获得用户身份。
这是正确的吗?
如果我需要用户登录信息,我每次都需要调用此函数吗?
谢谢你回复!
这是我的Startup.cs
public void ConfigureAuth(IAppBuilder app)
{
// Enable Application Sign In Cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = OAuthConfigur.AuthenticationType,
AuthenticationMode = AuthenticationMode.Passive,
LoginPath = new PathString(OAuthPaths.LoginPath),
LogoutPath = new PathString(OAuthPaths.LogoutPath),
ExpireTimeSpan = TimeSpan.FromMinutes(20)
});
// Setup Authorization Server
app.UseOAuthAuthorizationServer(new CustomerOAuthAuthorizationServerOptions());
}
答案 0 :(得分:0)
以防有人将来偶然发现这件事。我有同样的问题,当我意识到我已经设置了
时,我正在拔头发CookieSecure = CookieSecureOption.Always
在CookieAuthenticationOptions类上:/
所以很明显cookie只能通过https进行访问,因为我的本地环境没有使用https设置(以前是),它无法读取cookie。
答案 1 :(得分:0)
在将应用程序发布到生产服务器时,我遇到一种情况,调用httpContext.GetOwinContext()。Authentication .AuthenticateAsync(“ Application”)在IE浏览器中始终返回null。对于这种情况,请转到IE浏览器的Internet选项->可信站点,将您的身份服务器应用程序URL添加为可信站点。系统即可正常工作。