WebMatrix razor C#WebSecurity登录但Web.Security.IsAuthenticated没有返回正确的检查

时间:2012-10-18 13:36:33

标签: c# authentication razor login webmatrix

我有一个简单的问题。在我的代码中,我有这个:

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)中),以便我可以在使用发布的数据之前检查用户是否仍然登录。 #34; if(!WebSecurity.IsAuthenticated)"第一次打开页面的一小部分,询问登录信息。一旦重新输入并重新发布,检查将读取"的值。用户名"和"密码"并尝试重新登录它们。它就是这样,但是在它们应该被"登录后直接#34;它通过" ; 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:

好吧,我从未找到过&#34; if(!WebSecurity.IsAuthenticated)&#34;在显式&#34; if(WebSecurity.Login(用户名,密码,假))&#34;之后直接执行branch。分支被执行,但是我能够将一个单独的本地布尔变量绑定到逻辑并且一切正常工作(我检查以确保它将始终检查,除非JUST登录上面的分支,并检查以确保它事实上确实登录等。)。

如果有人能告诉我为什么这样做是为了我(以及其他遇到此页并遇到类似问题的人)教育的好处,我很乐意接受答案。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

我记得在MSDN或某个地方阅读,WebSecurity.IsAuthenticated在页面完全加载之前不起作用。这意味着如果您在页面中登录用户并且在相同的代码流中检查IsAuthenticated,则不会返回True。对于IsAuthenticated为True,必须重新加载页面或使用更好的练习;这是在登录成功后立即将用户重定向到另一个安全页面,并在该页面中检查IsAuthenticated。

希望它有所帮助。