会员提供商和asp:登录不正常

时间:2014-01-23 02:59:38

标签: c# asp.net webforms

我可能这样做是错的,但经过几个小时的谷歌搜索和搜索我再一次在StackOverflow问这里好人。我正在尝试将登录控制器添加到我的aspx WebForms站点。我做了以下事情:

  • 为WebConfig添加了适当的值。
  • 使用aspnet_regsql设置我的数据库(使用v4.0框架/站点正在运行4.5(这是一个问题吗?)
  • 添加了createUserWizard并检查用户是否已添加到数据库中。 (工作)
  • 检查应用程序ID /名称是否与在数据库中分配的用户相同。

我首先尝试在我的default.aspx网站上使用<asp:LoginView>结合<LoggedInTemplate>&amp; <AnonymousTemplate>。如果我提供的表单的凭据与在CreateUserWizard中注册的用户匹配,我没有收到任何错误,它会重定向我。但我仍然没有登录。如果我提供虚假凭据,它会给我一个“错误的用户名/密码错误”,因此它必须能够连接并验证凭据。

此时我有点迷失,因为没有出现错误,我不确定如何继续错误搜索。我还尝试添加第二个名为“login.aspx”的网站,结果相同。

我的网页

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

在从你们那里得到一些提示后我尝试了什么:

  • AnonymouseTemplate中添加了一个标签,它在User.Identity.isAuthenticated上运行测试,并返回false。即使很难,也不会出现错误。
  • 我安装了fiddler查看下面的截图,当我提交登录表单时它会获得一个cookie。

Fiddler HTTP data inspect.

1 个答案:

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