在我的.NET應用程序的核心中間件,我已經使用HttpContext
保存的Cookie下面的值,:然後在功能比較HttpContext的布爾我
app.Run(async context =>
{
// context.Response.Cookies.Append("se_id","5");
if(context.Request.Cookies["se_id"] == 5){
context.Items["isVerified"] = true;
}
await _next.Invoke(context);
});
,我是嘗試使用來檢查:
bool v = HttpContext.Items["isVerified"] == true;
if (v){
Console.WriteLine("Verified request? {0}", HttpContext.Items["isVerified"]);
}
else{
Console.WriteLine("NOT Verified request? {0}", HttpContext.Items["isVerified"]);
}
它給我的編譯錯誤:
Operator '==' cannot be applied to operands of type 'object' and 'bool' [netcoreapp1.1] Gets the Microsoft.AspNetCore.Http.HttpContext for the executing action. HttpContext ControllerBase.HttpContext
我可以雷德芬解決它在中間件荷蘭國際集團它,我把它定義爲:
context.Items["isVerified"] = "true";
然後檢查它的功能:
bool v = HttpContext.Items["isVerified"].ToString() == "true";
有沒有辦法避免使用它作爲Boolean
,而不是被迫將其轉換爲兩邊都有text
。
你嘗試過鑄造「isVerified」第一個布爾值?像'((bool)HttpContext.Items [「isVerified」])== true;' – th1rdey3
@ th1rdey3只是試過了,如果它是'NULL'它不會被轉換爲'bool' –