像真实登录表单一样提交虚拟表单



>我有一个 Web 应用程序,其登录表单为 ~/Account/Login.aspx ,我在其中实现了登录逻辑,它按预期工作。

现在,我们的平面设计师重塑了主页,在一个角落里放了一个小的html登录表单。我喜欢在主页上有另一个登录表单的想法,但我不知道如何使此表单像实际登录页面一样提交。我查看了真实登录页面生成的html,但似乎它没有事件使用<form>,所以我不能只调整输入框的名称和表单操作以匹配真实登录的名称和表单操作。

有没有办法在不重写所有隐藏代码的情况下启用此登录表单?

您可以使用 AJAX 调用成员资格服务以对用户进行身份验证:

您需要在 web.config 文件中启用AuthenticationServices

<configuration>
  <system.web.extensions>
    <scripting>
      <scriptResourceHandler enableCaching="false" enableCompression="false" />
      <webServices>
        <authenticationService enabled="true" requireSSL="false" />
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

然后在页面中:

<form id="form1" runat="server">
<div>
    <asp:ScriptManager runat="server" ID="sm">
    </asp:ScriptManager>
    <asp:LoginView runat="server">
        <AnonymousTemplate>
            <input type="tel" name="user" id="user" />
            <br />
            <input type="password" name="pass" id="pass" />
            <br />
            <input type="button" value="Login" name="login" id="login" onclick="attemptLogin(this);" />
        </AnonymousTemplate>
        <LoggedInTemplate>
            <input type="button" name="logout" id="logout" value="Logout" onclick="attemptLogout(this);" />
        </LoggedInTemplate>
    </asp:LoginView>
    <asp:LoginName FormatString="Welcome {0}!" runat="server" />
    <%--<asp:LoginStatus runat="server" />--%>
</div>
    <script>
        function attemptLogin(e) {
            Sys.Services.AuthenticationService.login(
                $get("user").value,
                $get("pass").value,
                false,
                null,
                null,
                function success(validCredentials, userContext, methodName) {
                    if (validCredentials == true) {
                        alert("user logged in");
                        window.location = window.location;
                    }
                    else {
                        alert("user name or password incorrect");
                    }
                }, function fail(error, userContext, methodName) {
                    alert(error.get_message());
                }, null);
        }
        function attemptLogout(e) {
            Sys.Services.AuthenticationService.logout(
                window.location,
                null,
                null,
                null
            );
        }
    </script>
</form>

或者,您可以使用登录逻辑公开 Web 服务,并使用 AJAX 调用该 Web 服务而不是AuthenticationService

另一种方法是使用登录控件创建一个UserControl,并将该控件放在主页上,处理UserControl代码中的登录事件

相关内容

最新更新