绑定用户和密码

时间:2011-07-22 15:55:33

标签: c# asp.net login-control

我对asp.net的登录控制有疑问。我在我的应用程序中定义了asp.Login和密码,我也在web配置中使用设置身份验证所需的信息进行了更改,但我想知道如何将用户和密码的字段绑定到数据库:I有这个疑问,因为在msdn文档中说"If you use the Login control with ASP.NET membership, you do not need to write code to perform authentication."

以下是我的观点代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/MasterSencillo.master"     AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="MapaPrueba.login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table class="tlbLogin">
    <tr>
        <td>
        <asp:Login ID="lgnMapZone" runat="server" DestinationPageUrl="~/Default.aspx" 
                LoginButtonText="Iniciar Sesion" 
                PasswordRequiredErrorMessage="Password requerido." RememberMeText="Recuerdáme" 
                TitleText="Inicio de Session " UserNameLabelText="Usuario:" 
                UserNameRequiredErrorMessage="Nombre de usuario Requerido">
            <LabelStyle CssClass="formatText" />
            <TitleTextStyle CssClass="formatText" />
            </asp:Login>
        </td>
    </tr>
</table>

我的Web.Config

<appSettings/>
<connectionStrings>
    <add name="MySqlConnection" connectionString="Data Source=.\SqlExpress;Initial Catalog=AtentoMIG;Integrated Security=True" />
</connectionStrings>
<system.web>
    <!-- 
        Set compilation debug="true" to insert debugging 
        symbols into the compiled page. Because this 
        affects performance, set this value to true only 
        during development.
    -->
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->
    <authentication mode="Forms">
        <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH"/>
    </authentication>
    <authorization>
        <deny users="?" />
    </authorization>
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
        <providers>
            <clear />
            <add
              name="SqlProvider"
              type="System.Web.Security.SqlMembershipProvider"
              connectionStringName="MySqlConnection"
              applicationName="MyApplication"
              enablePasswordRetrieval="false"
              enablePasswordReset="true"
              requiresQuestionAndAnswer="true"
              requiresUniqueEmail="true"
              passwordFormat="Hashed" />
        </providers>

6 个答案:

答案 0 :(得分:1)

ASP.Net有一个内置的身份验证系统,可以在aspnet_Users表中存储用户帐户。

您可以通过从Visual Studio命令提示符运行aspnet_regsql.exe来创建此表。

答案 1 :(得分:1)

您需要在web.config的Membership部分中指定的数据库中安装/运行asp.net成员资格模式(在您的情况下为MySqlConnection连接字符串)。

请查看this文章,了解分步说明。

答案 2 :(得分:1)

您必须使用自定义成员资格提供程序。 观看此视频create custom membership provider 还有关于custom member ship

的代码项目的文章

答案 3 :(得分:1)

我认为你需要阅读这篇文章。 http://msdn.microsoft.com/en-us/library/44w5aswa.aspx。这是设置SAP.bet成员资格提供程序的好文章。

答案 4 :(得分:1)

您的登录控件没有按钮。以下是从Visual Studio创建新Web时生成的代码:

    <asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
        <div class="accountInfo">
            <fieldset class="login">
                <legend>Account Information</legend>
                <p>
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                         CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p>
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                    <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p>
                    <asp:CheckBox ID="RememberMe" runat="server"/>
                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                </p>
            </fieldset>
            <p class="submitButton">
                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
            </p>
        </div>
    </LayoutTemplate>
</asp:Login>

如果成员资格设置正确,将按钮的CommandName设置为Login应该联系会员提供者并调用所有必要的方法来验证数据库中的用户。

答案 5 :(得分:1)

我不确定你对绑定用户名和密码的意思。但实际上不应该有任何约​​束力。

假设您配置了有效的成员资格提供程序,身份验证控件将立即生效。

控件本身并不依赖于任何特定的实现,它只是希望找到一个能够做有效的成员资格提供者需要做的事情的提供者。

默认情况下, SqlMembershipProvider 是默认提供程序(假设您没有更改machine.config文件)。但是你有能力换掉提供者。因此,当microsoft说你不需要做任何事情来使用控件时,它们是正确的,假设你想使用sql成员资格提供程序。

实际上默认情况下在页面上抛出一个登录控件,并加载页面,如果数据库不存在,sql express数据库将放入你的app_data文件夹。至少这是它的工作方式。