我想爲非網絡請求設置身份。目前我正在使用FormsAuthentication,並通過此User.Identity.Name獲取用戶身份。現在我必須通過API讓用戶登錄。我有用戶的用戶名/密碼,我如何爲此設置身份。設置非網絡請求身份
0
A
回答
0
您需要創建您自己的IPrincipal和IIdentity實現,然後在通過身份驗證後將其分配給HttpContext.User。
1
在非網絡環境,標識設置的線程上,可以通過訪問:
Thread.CurrentPrincipal.Identity
你可以用它來操縱身份或作爲瓦迪姆建議,完全符合自己的自定義覆蓋主要。
該代碼片段取自System.Web.Security.Membership
類,該類可與web和非web環境中的主體配合使用,並分析兩者如何一起使用。
public static string GetCurrentUserName()
{
if (HostingEnvironment.IsHosted)
{
HttpContext current = HttpContext.Current;
if (current != null)
{
return current.User.Identity.Name;
}
}
IPrincipal currentPrincipal = Thread.CurrentPrincipal;
if ((currentPrincipal != null) && (currentPrincipal.Identity != null))
{
return currentPrincipal.Identity.Name;
}
return string.Empty;
}
相關問題
- 1. 記住使用網絡請求進行身份驗證
- 2. ALDB身份驗證請求
- 3. Python ::請求身份驗證
- 4. Express.js POST空請求身份
- 5. 如何使用HTTPClient設置HTTP請求標題「身份驗證」?
- 6. 未在登錄請求中設置ASP.NET身份
- 7. 多網絡請求&網絡客戶端
- 8. Google身份驗證後沒有攜帶身份驗證cookie的React-Native網絡請求
- 9. Firebase身份驗證:signInWithEmailAndPassword返回錯誤代碼身份驗證/網絡請求失敗
- 10. 在某些頁面上禁用身份驗證,僅限本地網絡請求
- 11. react -ative如何設置網絡請求超時時間?
- 12. 設置請求標頭SharePoint網絡服務
- 13. 在網絡中爲授權令牌設置請求標頭
- 14. 設計要求身份驗證的Ajax請求
- 15. 嗅探網絡請求
- 16. Safari 8網絡請求空
- 17. GET網絡請求參數
- 18. Python網絡刮硒/請求
- 19. Three20:取消網絡請求
- 20. 重定向網絡請求
- 21. 重定向網絡請求
- 22. 網絡上的REST請求
- 23. 「網絡請求失敗」
- 24. 網絡請求失敗Android
- 25. Unity3D網絡請求流程
- 26. 的Process.Start網絡驅動器上,要求進行身份驗證
- 27. ASP.NET身份regenerats身份在每個請求
- 28. Python網絡/服務器身份驗證
- 29. Novell網絡上的Windows身份驗證
- 30. Windows網絡身份驗證正確