为什么Page.IsValid报告错误?

时间:2017-03-01 13:36:17

标签: c# asp.net twitter-bootstrap bootstrap-modal

我的页面顶部的下拉菜单中我的页面上有一个工作登录功能,但是由于移动设备上看似长时间运行的Bootstrap下拉菜单错误(我试了十几个'这个'为我工作的解决方案从未奏效过,我决定放弃下拉菜单并将登录表单移动到模态弹出窗口。
模态弹出很好,我可以输入登录详细信息,但是当我单击“登录”按钮时,模态会关闭,但不会登录。
我在登录代码中添加了断点,Page.IsValid每次都报告false 问题是,我根本没有改变任何代码,我设计的唯一变化是我将登录表单从下拉菜单移动到模态中,我想这只是将它放在一些不同的DIV标记并将数据切换从下拉列表更改为模态。

以下是模态中的代码。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    </div>
    <div class="modal-body">
    <div>
    <asp:LoginView runat="server" ViewStateMode="Disabled">
    <AnonymousTemplate>
    <span class="text"><i class="fa fa-user"></i>Login / Register</span>
    </AnonymousTemplate>
    <LoggedInTemplate>
    <span class="text"><i class="fa fa-user"></i>Logoff</span>
    <span class="equalizer-bar bg-red"></span>
    </LoggedInTemplate>
    </asp:LoginView>
    <asp:LoginView runat="server" ID="LoginView2" ViewStateMode="Disabled">
    <AnonymousTemplate>
    <li>
    <div class="form-group has-feedback">
    <asp:TextBox runat="server" ID="username" CssClass="form-control" placeholder="Username" TextMode="SingleLine" />
    <i class="fa fa-user form-control-feedback"></i>
    <asp:RequiredFieldValidator runat="server" ControlToValidate="username" ValidationGroup="rfvLogin" CssClass="text-danger" Display="Dynamic" ErrorMessage="Enter your username" />
    </div>
    <div class="form-group has-feedback">
    <asp:TextBox runat="server" ID="password" CssClass="form-control" placeholder="Password" TextMode="Password" />
    <i class="fa fa-lock form-control-feedback"></i>
    <asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="Password" ValidationGroup="rfvLogin" CssClass="text-danger" ErrorMessage="Enter your password" />
    </div>

    <div class="form-group has-feedback">
    <asp:CheckBox runat="server" ID="RememberMe" />
    <asp:Label runat="server" AssociatedControlID="RememberMe">&nbsp;Remember me?</asp:Label>
    </div>

    <button type="submit" runat="server" class="btn standard-hover-effect bg-red btn-block" onserverclick="LogIn">
    <span class="text">Log In <i class="fa fa-arrow-right"></i></span>
    </button>
    <h3 class="text-center">or</h3>

    <button type="submit" id="btnRegister" class="btn standard-hover-effect bg-red btn-block" runat="server" onserverclick="btnRegister_ServerClick">
    <span class="text">Sign Up <i class="fa fa-arrow-right"></i></span>
    </button>

    <p class="text-center"><a href="#">Forgot your password?</a></p>
    <h3 class="text-center margin-bottom-20">Sign in with your social account</h3>
    <ul class="list-inline text-center">
    <li>
    <a href="#"><span class="icon-circle"><i class="fa fa-facebook"></i></span></a>
    </li>
    <li>
    <a href="#"><span class="icon-circle"><i class="fa fa-twitter"></i></span></a>
    </li>
    <li>
    <a href="#"><span class="icon-circle"><i class="fa fa-google-plus"></i></span></a>
    </li>
    </ul>
    </li>
    </AnonymousTemplate>
    <LoggedInTemplate>
    <li>
    <button type="submit" runat="server" class="btn standard-hover-effect bg-red btn-block" id="btnLogOut" onserverclick="btnLogOut_ServerClick">
    <span class="text">
    <asp:LoginStatus runat="server" CssClass="color-white" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
    <i class="fa fa-arrow-right"></i>
    </span>
    </button>
    </li>
    </LoggedInTemplate>
    </asp:LoginView>
    </div>
    </div>
    </div>
    </div>
    </div>

再加上Login按钮调用的代码。

protected void LogIn(object sender, EventArgs e)
{
    string conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    Page.Validate("rfvLogin");
    if (Page.IsValid)
    {
        // Validate the user password
        var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

        // This doen't count login failures towards account lockout
        // To enable password failures to trigger lockout, change to shouldLockout: true
        var result = signinManager.PasswordSignIn((this.LoginView2.FindControl("username") as TextBox).Text, (this.LoginView2.FindControl("password") as TextBox).Text, (this.LoginView2.FindControl("RememberMe") as CheckBox).Checked, shouldLockout: false);

        switch (result)
        {
            case SignInStatus.Success:
                var loggedInUser = manager.FindByName(((this.LoginView2.FindControl("username") as TextBox).Text).ToString());
                string usrname = loggedInUser.UserName.ToString();
                Audit.LoginTimestamp(usrname);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                break;
            case SignInStatus.LockedOut:
                Response.Redirect("/Account/Lockout");
                break;
            case SignInStatus.RequiresVerification:
                Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString["ReturnUrl"], (this.LoginView2.FindControl("RememberMe") as CheckBox).Checked), true);
                break;
            case SignInStatus.Failure:
            default:
                FailureText.Text = "Invalid login attempt";
                ErrorMessage.Visible = true;
                break;
        }
    }
}

我发现的唯一建议是确保我包含一个验证组,但这没有任何区别 TIA

0 个答案:

没有答案