ASP.NET MVC 2 Membership users table mdf file
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 l开发者_开发技巧et 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
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.
精彩评论