我有一个网络应用程序,其登录表单位于~/Account/Login.aspx
,我实现了登录逻辑并按预期工作。需要提交真实登录表格的虚拟表单
现在,我们的平面设计师重塑了主页,在一个角落里放置了一个小的html登录表单。我喜欢在主页上有另一个登录表单的想法,但我不知道如何使这个表单提交像实际的登录页面。我查看了生成的真实登录页面的html内容,但它似乎没有使用<form>
,所以我不能只调整输入框的名称和表单操作以匹配真实的登录页面。
有没有办法让这个登录表单没有重写所有的代码?
我有一个网络应用程序,其登录表单位于~/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服务并调用Web服务,而不是AuthenticationService
的使用AJAX
另一种方法是创建与登录控件UserControl
并把主页处理登录事件在UserControl
的背后
如果我尝试登录它说:“尝试初始化System.Data.SqlClient.SqlConnection对象时发生错误。为连接字符串提供的值可能是错误的,或者它可能包含无效的语法”。可能与我使用表单身份验证的事实有关? – etuardu
您很可能需要在您的web.config文件中配置您的会员资格部分。我只是将这个示例上传到我的GitHub站点[Web.config](https://github.com/jupaol/SO-Answers/blob/master/Visual%20Studio%202012/src/SO/WebForms/WebForms_1/Web.config )和[ASPX页面](https://github.com/jupaol/SO-Answers/blob/master/Visual%20Studio%202012/src/SO/WebForms/WebForms_1/Topics/Membership/ScriptServices/LoginUsingScriptServices.aspx) – Jupaol
我没有使用任何角色/会员供应商 – etuardu
代码上控制如何才是真正的表单提交?使用GET请求?当然,它使用表单来发布用户名/密码。我们不能说如果我们没有看到'Login.aspx'标记。 –
这种方法的问题是,aspnet抽象化太多。这是登录标记:'的' –
etuardu