【问题标题】:binding user and password绑定用户和密码
【发布时间】:2011-07-22 15:55:32
【问题描述】:

我有一个关于asp.net 的登录控制的问题。我在我的应用程序中定义了 asp.Login 和密码,还使用设置身份验证所需的信息在 Web 配置中进行了更改,但我想知道如何将用户和密码字段绑定到数据库:我有这个疑问是因为在 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>

【问题讨论】:

  • 不要在 ASP.Net 中引用 System.Windows.Forms.dll。你会后悔的。

标签: c# asp.net login-control


【解决方案1】:

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

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

【讨论】:

  • 嗨,我运行了命令,但找不到包含此表的架构。也可以在特定模式中创建此表
【解决方案2】:

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

请查看this 文章以获取分步说明。

【讨论】:

    【解决方案3】:

    您必须使用自定义会员提供程序。 看看这个视频create custom membership provider 还有关于custom member ship的代码项目的文章

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

            <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 应该联系成员提供者并调用所有必要的方法来验证数据库中的用户。

        【讨论】:

          【解决方案6】:

          我不确定您所说的绑定用户名和密码究竟是什么意思。但实际上应该不需要做任何绑定。

          假设您配置了有效的会员资格提供商,身份验证控件将开箱即用。

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

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-29
            • 2017-04-20
            相关资源
            最近更新 更多