2014-01-23 73 views
0

我可能會這樣做都是錯誤的,但經過幾個小時的搜索和搜索後,我再一次向StackOverflow詢問了這些優秀的人。我試圖添加一個登錄控制器到我的aspx WebForms網站。我已完成以下操作:會員供應商和asp:登錄無法正常工作

  • 爲WebConfig添加了相應的值。
  • 設置我的數據庫與aspnet_regsql(使用V4.0框架/站點運行4.5(這是一個問題?)
  • 增加了一個CreateUserWizard控件,如果用戶被添加到數據庫中檢查。(工作)
  • 經過,如果應用程序ID /名稱相同的用戶獲取數據庫中指定。

我第一次嘗試有登錄表單中使用<asp:LoginView><LoggedInTemplate> & <AnonymousTemplate>結合我Default.aspx的網站。如果我公司供應具有與在Creat中註冊的用戶相匹配的憑據的表單eUserWizard,我沒有得到任何錯誤,並重定向我。但是我仍然沒有登錄。如果我提供了錯誤的憑證,它會給我一個「錯誤的用戶名/密碼錯誤」,因此它必須能夠連接和驗證憑證。

在這一點上,我有點失落,因爲沒有錯誤,我不確定我應該如何繼續錯誤搜索。我也嘗試添加第二個名爲「login.aspx」的網站,結果相同。

我的webconfig

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation targetFramework="4.5" debug="true"/> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <appSettings> 
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> 
    </appSettings> 
    <connectionStrings> 
    <add name="userconn" connectionString="Data Source=WIN-NVAAFR9N703\SQLEXPRESS;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=SomeUser;Password=ValidPassword" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

<system.web> 
    <machineKey validationKey="MANYNUMBERS" decryptionKey="EVEN_MORE_NUMBERS" validation="SHA1" decryption="AES" /> 
    <membership defaultProvider="AspNetSqlMembershipProvider"> 
    <providers> 
     <clear/> 
     <add name="AspNetSqlMembershipProvider" 
      type="System.Web.Security.SqlMembershipProvider" 
      connectionStringName="userconn" 
      passwordFormat="Encrypted" 
      enablePasswordRetrieval="true" 
      enablePasswordReset="false" 
      requiresQuestionAndAnswer="false" 
      requiresUniqueEmail="true" 
      maxInvalidPasswordAttempts="10" 
      minRequiredPasswordLength="5" 
      minRequiredNonalphanumericCharacters="0" 
      passwordAttemptWindow="30" 
      applicationName="/BlackJack" 
      /> 
    </providers> 
    </membership> 
</system.web> 
</configuration> 

登錄表單

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ProperLogin.ascx.cs" Inherits="BlacjJack.ProperLogin" %> 

<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400" rel="stylesheet" /> 

<asp:Login ID="Login1" runat="server" MembershipProvider="AspNetSqlMembershipProvider" DestinationPageUrl="~/default.aspx" VisibleWhenLoggedIn="False"> 
    <LayoutTemplate> 
     <div> 
      <div class="row half"> 
       <div class="12u"> 
        <asp:TextBox ID="UserName" runat="server" CssClass="text" Text="Username"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator> 
       </div> 

       <div class="12u"> 
        <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="text"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator> 
       </div> 

       <div class="12u"> 
        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal> 
       </div> 

       <div class="12u"> 
        <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." /> 

       </div> 
       <div class="12u"> 
        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log in" ValidationGroup="ctl00$Login1" CssClass="button" /> 
       </div> 
      </div> 
     </div> 
    </LayoutTemplate> 
</asp:Login> 

的CreateUserWizard

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ProperRegister.ascx.cs" Inherits="BlacjJack.ProperRegister" %> 

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" MembershipProvider="AspNetSqlMembershipProvider" Style="margin-right: 0px"> 
    <ContinueButtonStyle CssClass="button" /> 
    <WizardSteps> 
     <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"> 
     </asp:CreateUserWizardStep> 
     <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server"> 
     </asp:CompleteWizardStep> 
    </WizardSteps> 
</asp:CreateUserWizard> 

我TRIE什麼從你們那裏得到一些提示後d:

  • 添加一個標籤,在AnonymouseTemplate,它運行在User.Identity.isAuthenticated測試,並返回false。即使很難沒有錯誤的情況。
  • 我安裝了fiddler檢查下面的截圖,它在我提交登錄表單時得到一個cookie。

Fiddler HTTP data inspect.

+0

如果我沒有記錯,登錄控制是不負責保持會話狀態。您需要設置用戶登錄的會話變量,或以其他方式保存用戶登錄,然後在每次頁面加載時檢查以保持狀態。 –

+1

我以爲它使用.ASPXAUTH會話coockie/FormsAuthentication自動處理? http://msdn.microsoft.com/en-us/library/7t6b43z4(v=vs.100).aspx – chriskvik

+0

你是對的。不確定在這一個。 –

回答

0

其實我解決它。看起來我缺少WebConfig中的身份驗證方法。 我加:

<authentication mode="Forms"> 
    <forms name="Login1" loginUrl="~/default.aspx" path="/" 
    cookieless="UseCookies" /> 
</authentication> 

而且一切似乎現在工作正常。

的Web.Config現在看起來像這樣:

<?xml version="1.0"?> 

<configuration> 
    <system.web> 
    <compilation targetFramework="4.5" debug="true"/> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <appSettings> 
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> 
    </appSettings> 
    <connectionStrings> 
    <add name="userconn" connectionString="Data Source=WIN-NVAAFR9N703\SQLEXPRESS;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=usr;Password=pwd" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

    <system.web> 

    <authentication mode="Forms"> 
     <forms name="Login1" loginUrl="~/default.aspx" path="/" 
     cookieless="UseCookies" /> 
    </authentication> 

    <machineKey validationKey="NUMBERS" decryptionKey="NUMHBERS" validation="SHA1" decryption="AES" /> 

    <membership defaultProvider="AspNetSqlMembershipProvider3"> 
    <providers> 
     <add name="AspNetSqlMembershipProvider3" 
      type="System.Web.Security.SqlMembershipProvider" 
      connectionStringName="userconn" 
      passwordFormat="Encrypted" 
      enablePasswordRetrieval="true" 
      enablePasswordReset="false" 
      requiresQuestionAndAnswer="false" 
      requiresUniqueEmail="true" 
      maxInvalidPasswordAttempts="10" 
      minRequiredPasswordLength="5" 
      minRequiredNonalphanumericCharacters="0" 
      passwordAttemptWindow="30" 
      applicationName="/BlackJack" 
      /> 
    </providers> 
    </membership> 
</system.web> 
</configuration>