2012-09-25 36 views
0

我有一個網絡應用程序,其登錄表單位於~/Account/Login.aspx,我實現了登錄邏輯並按預期工作。需要提交真實登錄表格的虛擬表單

現在,我們的平面設計師重塑了主頁,在一個角落裏放置了一個小的html登錄表單。我喜歡在主頁上有另一個登錄表單的想法,但我不知道如何使這個表單提交像實際的登錄頁面。我查看了生成的真實登錄頁面的html內容,但它似乎沒有使用<form>,所以我不能只調整輸入框的名稱和表單操作以匹配真實的登錄頁面。

有沒有辦法讓這個登錄表單沒有重寫所有的代碼?

+0

代碼上控制如何纔是真正的表單提交?使用GET請求?當然,它使用表單來發布用戶名/密碼。我們不能說如果我們沒有看到'Login.aspx'標記。 –

+0

這種方法的問題是,aspnet抽象化太多。這是登錄標記:'的' – etuardu

回答

1

你可以使用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的背後

+0

如果我嘗試登錄它說:「嘗試初始化System.Data.SqlClient.SqlConnection對象時發生錯誤。爲連接字符串提供的值可能是錯誤的,或者它可能包含無效的語法」。可能與我使用表單身份驗證的事實有關? – etuardu

+0

您很可能需要在您的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/Scr​​iptServices/LoginUsingScriptServices.aspx) – Jupaol

+0

我沒有使用任何角色/會員供應商 – etuardu