How to configure member ship with a database other

2019-09-12 05:34发布

问题:

I created one database and tables to store the user login values and credentials. asp.net is providing aspnet_regsql tool to create a database for the membership related activities. But I dont want to use it. Thats why I created another database. Now I want to connect this database to my project. I changed in web.config file for the connectionstring parameter to my newly created database. But I am unable to login. It is giving following error message.

Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion'

How to work with this. Is there any step by step procedures are there!! If so please provide. Is there any thing to change rather than the connection string in the web.config file?

回答1:

You need to create a membership provider to connect to your custom tables for authentication. MSDN has some documentation on the subject. You can also view a video on the subject at ASP.NET. Here are the links.

  • http://msdn.microsoft.com/en-us/library/f1kyba5e(v=vs.100).aspx
  • http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-a-custom-membership-provider

The main method for validation is going to be the ValidateUser method, you will override this method to provide authentication.

public sealed class CustomMembershipProvider : MembershipProvider
{
    // implement other methods

    public override bool ValidateUser(string username, string password)
    {
        try
        {
            var user = // GET USER OBJECT HERE
            if (user != null)
            {
                string name =  // set username

                // Set your forms authentication ticket
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.ID.ToString(), DateTime.Now, DateTime.Now.AddMinutes(30), false, name, FormsAuthentication.FormsCookiePath);

                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                HttpContext.Current.Response.Cookies.Add(authCookie); 
                return true;                    
            }
        }
        catch
        {
        }

        return false;
    }

    // Other implementations
}

If you have roles in your application you may also want to implement a custom role provider:

http://msdn.microsoft.com/en-us/library/8fw7xh74(v=vs.100).aspx