[英]WebMatrix razor C# WebSecurity Logging in but Web.Security.IsAuthenticated not returning right check
我有一個快速的問題。 在我的代碼中,我有:
if(!(username.IsEmpty() || password.IsEmpty()))
{
if (WebSecurity.UserExists(username) && WebSecurity.GetPasswordFailuresSinceLastSuccess(username) > 4 && WebSecurity.GetLastPasswordFailureDate(username).AddSeconds(120) > DateTime.UtcNow)
{
Session["gActionMessage"] = "You're account has been locked due to too many failed login attempts. " +
"Please try again in 2 minutes.";
Session["gActionMessageDisplayed"] = "not";
Response.Redirect("~/");
return;
}
if(WebSecurity.Login(username, password, false))
{
errorMessage = "";
}
}
if (!WebSecurity.IsAuthenticated)
{
errorMessage = "You are no longer logged in. Please supply your username and password credentials along with the rest of the form data.";
}
當我嘗試使用此代碼時(登錄后的檢查實際上是在if(IsPost)內,以便我可以在使用發布的數據之前檢查用戶是否仍在登錄。如果檢查失敗,則檢查“ if(!”)。 WebSecurity.IsAuthenticated)”,頁面的一小部分會打開,詢問登錄信息。重新輸入並重新發布后,檢查將讀取“用戶名”和“密碼”的值,並嘗試重新登錄只是這樣做,但是應該在他們應該被“登錄”之后立即傳遞“ if(!WebSecurity.IsAuthenticated)”分支並執行其內容。我是否要檢查“ if(!WebSecurity.IsAuthenticated)” “到不久?頁面是否必須完成加載才能真正被認證為個人?
我只是似乎無法查明發生這種情況的原因,也無法在研究中找到任何幫助。
謝謝大家的幫助!
更新:
我已經發布了出現在以下代碼下方的代碼:
if((Roles.IsUserInRole((WebSecurity.CurrentUserName), "Locked")) || (Roles.IsUserInRole((WebSecurity.CurrentUserName), "AdminLocked")))
{
Session["gActionMessage"] = "Your account is locked. ";
Session["gActionMessage"] += "Please contact an administrator and ask that your account be approved.";
Session["gActionMessageDisplayed"] = "not";
Response.Redirect("~/");
}
}
}
@RenderPage("~/Shared/HeaderLayout.cshtml")
<div>
<span><button type="button" class="btn" onclick="location.href='/IntroPage.cshtml'">Main Page</button></span><span class="heading">Lookup Entry</span><span><button type="button" class="btn" onclick="javascript:document.getElementById('searchForm').submit()">Search</button></span></br></br>
<span style="font-size: 3em; color: #808080;">________________________________________________</span></br></br></br>
</div>
<div id="FormHolder">
<form id="searchForm" class="searchForm" method="post" action="">
@{
if (errorMessage != "")
{
<div class="errorMessageWrapper"><span style="font-style: italic; font-weight: 700;">ERROR:</span><br/>@errorMessage<br/><br/>
@if(!WebSecurity.IsAuthenticated && success==false)
{
<table class="accInterfaceTable">
<tr>
<td class="accInterfaceLabelCell">
<label for="username">Email:</label>
</td>
<td class="accInterfaceInputCell">
<input type="text" id="username" name="username" /><br/><br/>
</td>
</tr>
<tr>
<td class="accInterfaceLabelCell">
<label for="password">Password:</label>
</td>
<td>
<input type="password" id="password" name="password" /><br/><br/>
</td>
</tr>
</table><br/><br/>
<input type="hidden" id="hiddenLoginSubmit" name="hiddenLoginSubmit" value="" />
<input type="submit" class="btn" value="Log In" />
}
</div><br/>
}
}
<table class="searchTable">
<tr>
<th>Search Field<br/><br/></th>
<th></th>
<th>Search Condition<br/><br/></th>
<th></th>
<th>Search Parameter<br/><br/></th>
</tr>
在此下方還有更多html,但這只是多余的html形式。
UPDATE2:
好吧,我從未發現執行顯式的“ if(WebSecurity.Login(用戶名,密碼,false))”分支后直接執行“ if(!WebSecurity.IsAuthenticated)”分支的原因,但是我能夠將一個單獨的本地布爾變量綁定到邏輯上,一切現在都可以正常工作(我檢查以確保它將始終進行檢查,除非僅通過上述分支登錄,然后檢查以確保它實際上已登錄等)。 。
如果有人能告訴我為什么這樣做是為了我(以及翻閱此頁面並遇到類似問題的其他人)的利益,我將很樂意接受答案。
謝謝您的幫助!
我記得在MSDN或其他地方閱讀過,WebSecurity.IsAuthenticated在頁面完全加載后才起作用。 這意味着,如果您在頁面中以相同的代碼流形式登錄用戶並檢查IsAuthenticated,則它將不會返回True。 為了使IsAuthenticated為True,必須重新加載頁面或使用更好的做法。 這將在登錄成功后立即將用戶重定向到另一個安全頁面,並在該頁面中檢查IsAuthenticated。
希望能幫助到你。
檢查您的webconfig,您必須啟用表單身份驗證:
在其中添加以下代碼段
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="3600" />
</authentication>
注釋掉它是否在您的webconfig中:
<!--<modules>
<remove name="FormsAuthentication" />
</modules>-->
現在您可以檢查
WebSecurity.CurrentUserName,WebSecurity.CurrentUserId和,WebSecurity.IsAuthenticated標志;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.