![](/img/trans.png)
[英]Getting the User.Identity during service Resolution in Autofac and WebAPI 2 Owin
[英]c# owin can't get User.Identity after signin
完成登錄代碼后
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之后,瀏覽器中有cookie。
但是,當Authorize屬性沒有授權時。
在我的自定義“授權”操作過濾器中,
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());
}
以防萬一將來有人偶然發現這一點。 我遇到了同樣的問題,當我意識到自己已經設置好
CookieSecure = CookieSecureOption.Always
在CookieAuthenticationOptions類上:/
因此很明顯,cookie只能通過https訪問,並且因為我的本地環境未使用https設置(過去是),因此無法讀取cookie。
在將應用程序發布到生產服務器時,我遇到一種情況,調用httpContext.GetOwinContext()。Authentication .AuthenticateAsync(“ Application”)在IE瀏覽器中始終返回null。 對於這種情況,請轉到IE瀏覽器的Internet選項->可信站點,將您的身份服務器應用程序URL添加為可信站點。 系統即可正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.