虚拟表单提交像真正的登录表单 [英] Dummy form to submit like the real login form

查看:260
本文介绍了虚拟表单提交像真正的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序的登录表单是〜/帐号/的Login.aspx ,在这里我实现登录逻辑和它的作品如预期。

现在,我们的平面设计师重塑主页在一个角落里放一个小的HTML登录表单。我喜欢有在家里的另一个页面登录表单的想法,但我不知道如何使这种形式也像提交的实际登录页面。我看看真正的登录页面生成的HTML内,但它似乎不活动使用<形式> ,所以我不能随便调整输入框的名称和形式的行动,以符合实际登录的人。

有没有一种方法,使这个登录表单背后没有重写所有的code?


解决方案

您可以使用AJAX调用会员服务,以便用户进行身份验证:

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

 <结构>
  < System.Web.Extensions程序>
    <脚本>
      < scriptResourceHandler enableCaching =false的enableCom pression =FALSE/>
      <&Web服务GT;
        <启用的AuthenticationService =真正的requireSSL =FALSE/>
      < / WebServices的>
    < /脚本>
  < /system.web.extensions>
< /结构>

和随后在页:

 <表ID =form1的=服务器>
< D​​IV>
    < ASP:的ScriptManager =服务器ID =SM>
    < / ASP:ScriptManager的>
    < ASP:LoginView =服务器>
        < AnonymousTemplate>
            <输入类型=电话NAME =用户ID =用户/>
            < BR />
            <输入类型=密码NAME =通行证ID =通/>
            < BR />
            <输入类型=按钮值=登录NAME =登陆ID =登陆的onclick =attemptLogin(本); />
        < / AnonymousTemplate>
        <&显示LoggedInTemplate GT;
            <输入类型=按钮NAME =注销ID =注销值=注销的onclick =attemptLogout(本); />
        < /&显示LoggedInTemplate GT;
    < / ASP:LoginView>
    < ASP:LoginName将formatString中=欢迎{0}! =服务器/>
    <% - < ASP:LoginStatus =服务器/> - %GT;
< / DIV>
    <脚本>
        功能attemptLogin(五){
            Sys.Services.AuthenticationService.login(
                $获得(用户)。价值,
                $获得(通行证)。价值,
                假,
                空值,
                空值,
                函数成功(validCredentials,的UserContext,方法名){
                    如果(validCredentials ==真){
                        警报(用户登录);
                        window.location的= window.location的;
                    }
                    其他{
                        警报(用户名或密码不正确);
                    }
                },功能失效(错误的UserContext,方法名){
                    警报(error.get_message());
                }, 空值);
        }        功能attemptLogout(五){
            Sys.Services.AuthenticationService.logout(
                window.location的,
                空值,
                空值,
                空值
            );
        }
    < / SCRIPT>
< /表及GT;

另外,你可以用暴露登录逻辑的Web服务并调用Web服务,而不是的AuthenticationService 使用AJAX

另一种方法是创建一个用户控件通过登录控件,并将该控件的主页上处理登录事件在用户控件背后

i have a web application whose login form is in ~/Account/Login.aspx, where I implemented the login logic and it works as expected.

Now, our graphic designer reshaped the home page putting in a corner a small html login form. I liked the idea of having another login form in the home page, but I don't know how to make this form do submit like the actual login page. I had a look inside the generated html of the real login page but it seems that it doesn't event use a <form>, so I can't just adjust the input boxes' names and form action to match the real login's ones.

Is there a way to enable this login form without rewriting all the code behind?

解决方案

You could use AJAX to call the membership services in order to authenticate the user:

You would need to enable the AuthenticationServices in the web.config file

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

And then in a page:

<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>

Alternatively, you could expose a web service with the login logic and call that web service instead of the AuthenticationService using AJAX

Another way would be to create a UserControl with the login controls and put that control on the home page handling the login events in the UserControl's code behind

这篇关于虚拟表单提交像真正的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆