ASP.NET MVC 2 会员用户表 mdf 文件

发布于 2024-09-11 04:01:22 字数 4806 浏览 3 评论 0原文

我有一个 MVC 2 项目,其中 App_Data 目录中有我自己的 *.mdf 文件。 *.mdf 文件包含我的普通表,但不包含用户表。

是否可以将用户表添加到我的 *.mdf 文件中并使用它而不是简单的注册表单? (如果是这样:这怎么能完成?)

因为我不知道如何执行上面的这些操作,所以我进行了搜索,只找到了将会员资格中的构建添加到我的 mdf 文件中的步骤,然后我尝试了:

我尝试添加正常构建在我的 *.mdf 文件的成员资格中,

aspnet_regsql.exe -C CONSTRINGHERE -d PATHTOMDFFILEHERE -A all

我将我的 Web.config 文件更改为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="CompanyDBEntities" connectionString="metadata=res://*/Models.CompanyDB.csdl|res://*/Models.CompanyDB.ssdl|res://*/Models.CompanyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CompanyData.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <membership defaultProvider="CompanyDBSqlProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear />
        <add name="CompanyDBSqlProvider"
            type="System.Web.Security.SqlMembershipProvider"
            connectionStringName="CompanyDBEntities"
            applicationName="/"
            enablePasswordRetrieval="false"
            enablePasswordReset="true"
            requiresQuestionAndAnswer="true"
            requiresUniqueEmail="true"
            passwordFormat="Hashed"
            maxInvalidPasswordAttempts="5"
            minRequiredPasswordLength="6"
            minRequiredNonalphanumericCharacters="0"
            passwordAttemptWindow="10"
            passwordStrengthRegularExpression=""
            />

      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" 
             connectionStringName="CompanyDBEntities" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" 
             connectionStringName="CompanyDBEntities" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <pages>
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我没有修改任何其他内容。我没有改变AccountController之类的东西。 我构建并运行了我的 MVC 2 应用程序,然后转到 LogOn 并注册了一个帐户,但卡在那里:

Account creation was unsuccessful. Please correct the errors and try again.

    * The password retrieval answer provided is invalid. Please check the value and try again.

摘要:

我认为在我的 *.mdf 文件中包含一个 Users 表会更好,但我不知道如何使用自己的用户表进行注册/登录/注销/会员资格/等操作。 因此,我尝试将成员身份添加到我自己的 *.mdf 文件中,以便所有内容都位于该 *.mdf 文件中,但这也给我带来了问题。

也许这个信息很重要: 将来我希望能够让用户通过 Hotmail、Gmail、OpenId 注册和登录,除了现在的基本注册表单之外,我应该需要更多字段,如网站、昵称、头像等。还需要在网站上显示用户的个人资料页面。

有人可以帮我吗?

PS:我没有 ASP.NET 中的会员资格经验:s

I have an MVC 2 project where I have my own *.mdf file in the App_Data directory. The *.mdf file contains my normal tables but not a users table.

Is it possible to add a Users table to my *.mdf file and work with that instead of the simple register form? (if so: how could this be done?)

Because I don't know how to do these things above, I searched and only found steps to add build in Membership to my mdf file and I tried it:

I tried to add normal build in Membership to my *.mdf file with this:

aspnet_regsql.exe -C CONSTRINGHERE -d PATHTOMDFFILEHERE -A all

and I changed my Web.config file to this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="CompanyDBEntities" connectionString="metadata=res://*/Models.CompanyDB.csdl|res://*/Models.CompanyDB.ssdl|res://*/Models.CompanyDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CompanyData.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <membership defaultProvider="CompanyDBSqlProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear />
        <add name="CompanyDBSqlProvider"
            type="System.Web.Security.SqlMembershipProvider"
            connectionStringName="CompanyDBEntities"
            applicationName="/"
            enablePasswordRetrieval="false"
            enablePasswordReset="true"
            requiresQuestionAndAnswer="true"
            requiresUniqueEmail="true"
            passwordFormat="Hashed"
            maxInvalidPasswordAttempts="5"
            minRequiredPasswordLength="6"
            minRequiredNonalphanumericCharacters="0"
            passwordAttemptWindow="10"
            passwordStrengthRegularExpression=""
            />

      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" 
             connectionStringName="CompanyDBEntities" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" 
             connectionStringName="CompanyDBEntities" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <pages>
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

I did not modify anything else. I did not change the AccountController or something.
I builded and ran my MVC 2 application and went to LogOn and registered an account but got stuck there:

Account creation was unsuccessful. Please correct the errors and try again.

    * The password retrieval answer provided is invalid. Please check the value and try again.

Summary:

I thought that it would be better to have a Users table in my *.mdf file but I don't know how to do the register/login/logout/membership/etc... things with an own users table.
So I tried adding Membership to my own *.mdf file so that everything would be in that one *.mdf file but that gives me also problems.

Maybe this info is important:
In the future I want it to be possible to let users register and login via Hotmail, Gmail, OpenId and I should need more fields like website, nickname, avatar, etc... other than the basic register form now. Also would need to show a profilepage from the user on the website.

Could anybody help me out?

PS: I don't have experience with Membership in ASP.NET :s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2024-09-18 04:01:22

我遇到了同样的问题。您可以通过执行以下操作来自定义用户注册表:

首先,在 web.config 中配置 sqlmemberprovider 服务:

  ' add connectionStringName="PhotoSocialMemberService" applicationName="PhotoSocial"
          minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" 
          requiresQuestionAndAnswer="true"
          name="PhotoSocailMember" type="System.Web.Security.SqlMembershipProvider" /'

编辑 AccountModel.cs,查找
方法“CreateUser”,
抽象类应该是这样的:

  public abstract class MembershipProvider : ProviderBase  
    {...
     public abstract MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status); 
 }

在 AccountModel.cs 中,您可以找到抽象方法的实现,

public interface IMembershipService
{
    int MinPasswordLength { get; }

    bool ValidateUser(string userName, string password);
    MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordquestion, string passwordanswer);
    bool ChangePassword(string userName, string oldPassword, string newPassword);
}

并在

 public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordquestion, string passwordanswer)  
        {  
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");  
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");  
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");  

            MembershipCreateStatus status;  
            _provider.CreateUser(userName, password, email, passwordquestion, passwordanswer, true, null, out status);  
            return status;  
        }

其中添加任意数量的参数。

在AccountModel.cs中的RegisterModel中编辑注册模型,添加你想要放入registerModel的字符串:

     public class RegisterModel
     {[Required]
                [DisplayName("User name")]
                public string UserName { get; set; }

                [Required]
                [DataType(DataType.EmailAddress)]
                [DisplayName("Email address")]
                public string Email { get; set; }

                [Required]
                [ValidatePasswordLength]
                [DataType(DataType.Password)]
                [DisplayName("Password")]
                public string Password { get; set; }

                [Required]
                [DataType(DataType.Password)]
                [DisplayName("Confirm password")]
                public string ConfirmPassword { get; set; }

                [Required]
                [DisplayName("Password security question")]
                public string PasswordQuestion { get; set; }

                [Required]
                [DataType(DataType.Password)]
                [DisplayName("Password security answer")]
                public string PasswordAnswer { get; set; }

            }

最后,重写Register.aspx页面,添加html标签:

Html.LabelFor(m => m.PasswordQuestion)

最重要的是,你需要将_provider从readonly更改为private

 public class AccountMembershipService : IMembershipService
    {
        private MembershipProvider _provider;
}

即可通过此过程添加您的验证方法。

I encountered the same problem. You can customize the user register table by doing these:

First of all, configure the sqlmemberprovider service in web.config:

  ' add connectionStringName="PhotoSocialMemberService" applicationName="PhotoSocial"
          minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" 
          requiresQuestionAndAnswer="true"
          name="PhotoSocailMember" type="System.Web.Security.SqlMembershipProvider" /'

Edit the AccountModel.cs., look for
the method "CreateUser", the
abstract class should be like this:

  public abstract class MembershipProvider : ProviderBase  
    {...
     public abstract MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status); 
 }

in the AccountModel.cs you can find implementation of the abstract method in

public interface IMembershipService
{
    int MinPasswordLength { get; }

    bool ValidateUser(string userName, string password);
    MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordquestion, string passwordanswer);
    bool ChangePassword(string userName, string oldPassword, string newPassword);
}

and in

 public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordquestion, string passwordanswer)  
        {  
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");  
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");  
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");  

            MembershipCreateStatus status;  
            _provider.CreateUser(userName, password, email, passwordquestion, passwordanswer, true, null, out status);  
            return status;  
        }

add as many as parameters as you like.

Edit the register model in RegisterModel in AccountModel.cs, add string you want to put into the registerModel:

     public class RegisterModel
     {[Required]
                [DisplayName("User name")]
                public string UserName { get; set; }

                [Required]
                [DataType(DataType.EmailAddress)]
                [DisplayName("Email address")]
                public string Email { get; set; }

                [Required]
                [ValidatePasswordLength]
                [DataType(DataType.Password)]
                [DisplayName("Password")]
                public string Password { get; set; }

                [Required]
                [DataType(DataType.Password)]
                [DisplayName("Confirm password")]
                public string ConfirmPassword { get; set; }

                [Required]
                [DisplayName("Password security question")]
                public string PasswordQuestion { get; set; }

                [Required]
                [DataType(DataType.Password)]
                [DisplayName("Password security answer")]
                public string PasswordAnswer { get; set; }

            }

Last, rewrite the Register.aspx page, add the html tag:

Html.LabelFor(m => m.PasswordQuestion)

Most important, you need to change the _provider from readonly to private

 public class AccountMembershipService : IMembershipService
    {
        private MembershipProvider _provider;
}

You can add your validation methods through this progress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文