WebMatrix Razor C#WebSEcurity登录in web.security.isauthenticat



我有一个快速的问题。在我的代码中,我有:

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.isauthenticatienatient)"第一次,该页面的一小部分打开,要求登录信息。一旦重新输入并重新进行了重新输入,该检查就会读取"用户名"one_answers"密码"的值,并尝试尝试将它们重新加入。它确实是这样做的,但是在应该"登录"之后直接通过" if(!websecurity.isauthenticatienatienatienatienatienatienatienatienatienatienatienatienatienated)"分支并执行其内容。我在检查(!。

我似乎无法指出为什么会发生这种情况,也无法在研究中找到任何帮助。

谢谢,大家,任何帮助!

更新:

我已经发布了以下代码下方的代码:

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.isauthenticatienated)"分支在显式之后直接执行" if(websecurity.login.login(用户名,密码,false))"分支已执行,但是i能够将逻辑上的单独的本地布尔变量绑在一个单独的本地布尔变量中,并且一切都可以正常工作(我检查了以确保它始终检查,除非仅与上面的分支签名,并进行了检查,并确保它确实在登录,等)。

如果有人能告诉我为什么这是为了我的受益(以及其他任何人遇到并遇到了类似问题的人)教育,我将很乐意接受答案。

感谢您的帮助!

我记得在MSDN或某个地方阅读,websecurity.isauthenticated直到页面满载后才起作用。这意味着,如果您在页面中登录用户,并且在相同的代码流中,您会检查ISAUTHENTICATICATICENT,它将不会返回true。为了正确的态度为真,必须重新加载页面或使用更好的实践;登录成功并在该页面检查等法清单后,将用户重定向到另一个有担保页面。

希望它有帮助。

检查您的webconconfig,您必须启用表单身份验证:

添加以下片段

   <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="3600" />
    </authentication>

评论是否在您的webconfig中:

<!--<modules>
      <remove name="FormsAuthentication" />
</modules>-->

现在您可以检查

websecurity.currentusername,websecurity.currentuserid and websecurity.isauthenticationed标志;

最新更新