1

我正在尝试将 Visual Studio 中的登录控件与我的 Access 数据库一起使用。我不确定如何使用这两个。这是我到目前为止所拥有的:

登录.aspx:

<%@ Page Title="Login" Language="C#" MasterPageFile="~/EditSite.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Login ID="Login1" runat="server">
    </asp:Login>
</asp:Content>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.OleDb;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)

        ViewState["LoginErrors"] = 0;
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
         if (YourValidationFunction(Login1.UserName, Login1.Password))
         {
             e.Authenticated = true;
             Login1.TitleText = "Successfully Logged In";
        } else {
            e.Authenticated = false;
        }
    }

    protected void Login1_LoginError(object sender, EventArgs e)
    {

        if (ViewState["LoginErrors"] == null)

            ViewState["LoginErrors"] = 0;

        int ErrorCount = (int)ViewState["LoginErrors"] + 1;

        ViewState["LoginErrors"] = ErrorCount;

        if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))

            Response.Redirect(Login1.PasswordRecoveryUrl);

    }

    private bool YourValidationFunction(string UserName, string Password)

    {

        bool boolReturnValue = false;

        string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";

        OleDbConnection con = new OleDbConnection(constr);

        String SQLQuery = "SELECT UserName, Password FROM Login";

        OleDbCommand com = new OleDbCommand(SQLQuery, con);


        OleDbDataReader Dr;

        con.Open();

        Dr = com.ExecuteReader();

        while (Dr.Read())

        {

            if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))

            {

                boolReturnValue = true;

            }

            Dr.Close();

            return boolReturnValue;

        }

        return boolReturnValue;

       }

    }

这是网络配置:

<configuration>
  <configSections>
    <section name="resizer" type="ImageResizer.ResizerSection" requirePermission="false" />
  </configSections>
  <system.web>
    <customErrors mode="Off" />
    <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>
    <pages>
       <controls>
         <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
       </controls>
    </pages>
     <profile defaultProvider="DefaultProfileProvider">
       <providers>
         <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
       </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
         <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
<!--
        If you are deploying to a cloud environment that has multiple web server instances,
        you should change session state mode from "InProc" to "Custom". In addition,
        change the connection string named "DefaultConnection" to connect to an instance
        of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
  -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
       <providers>
         <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
       </providers>
    </sessionState>
  <httpModules><add name="ImageResizingModule" type="ImageResizer.InterceptModule" /></httpModules></system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="AjaxControlToolkit" publicKeyToken="28f01b0e84b6d53e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.40412.0" newVersion="4.1.40412.0" />
      </dependentAssembly>
     </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-TravelJoansBlog-20130718193109;Integrated Security=SSPI"
  providerName="System.Data.SqlClient" />
    <add name="TravelJoansDBSQLConnectionString" connectionString="Data Source=DCSLAPTOP\TRAVELJOANS;Initial Catalog=TravelJoansDBSQL;Integrated Security=True"
  providerName="System.Data.SqlClient" />
  </connectionStrings>
<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules>
    <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
  </modules>
  <httpErrors errorMode="Detailed" />
  <asp scriptErrorSentToBrowser="true"/>
</system.webServer>
  <resizer>
    <plugins>
       <add name="MvcRoutingShim" />
       <add name="DiskCache" />
       <add name="PrettyGifs" />
       <add name="SimpleFilters" />
    </plugins>
  </resizer>
</configuration>

任何帮助将不胜感激。当我单击登录按钮时,它给出了一个关于找不到网络相关实例的错误。我在文件夹上设置了安全设置,以便所有人都能完全控制。

4

2 回答 2

1

登录控制默认使用ASPNETDB.mdf文件中的成员表。如果要使用自定义数据库,如access数据库,则需要实现Membership Provider

请参阅以下页面:

如何:实现自定义成员资格用户

成员资格提供程序实施示例

示例代码: http: //www.devx.com/assets/sourcecode/13854.zip

于 2013-09-17T13:04:40.657 回答
0

我决定使用不同的路线。我发现了这个并且能够让它为我工作。如何在不使用 MembershipProvider 的情况下使用 ASP.NET 登录控件?

于 2013-09-17T00:12:37.073 回答