Hi Calcado,
Sorry I can't be of an yhelp but I wonder, how's your setup? I'm trying to get the custom membership provider working but I don't understand where to find the SELECT statement in the custommembershipprovider.cs file. Could you please paste yours?
Mine looks like below (copied from support.microsoft.com). Thanks!
Pettrer
Here's the code that is in the .cs file (I'm trying out the ValidateUser method):
using System;
using System.Web;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Collections.Specialized;
using System.Data.SqlClient;
class CustomMembershipProvider : MembershipProvider
{
public override void Initialize(string name, NameValueCollection config)
{
// Verify that config isn't null
if (config == null)
throw new ArgumentNullException("config");
// Assign the provider a default name if it doesn't have one
if (String.IsNullOrEmpty(name))
name = "AspNetCustomMembershipProvider";
// Add a default "description" attribute to config if the
// attribute doesn't exist or is empty
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "Custom SQL Provider");
}
// Call the base class's Initialize method
base.Initialize(name, config);
}
public override bool ValidateUser(string username, string password)
{
SqlConnection cnn = null;
SqlCommand cmd = null;
bool userExists = true;
try
{
cnn = new SqlConnection();
cnn.ConnectionString = "myConnectionString";
cnn.Open();
string selectQry = "Select query for username and password";
cmd = new SqlCommand(selectQry, cnn);
SqlDataReader rdr = cmd.ExecuteReader();
if (!rdr.Read())
userExists = false;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
cnn.Close();
}
return userExists;
}
public override MembershipUser CreateUser(string username, string
password, string email, string passwordQuestion, string
passwordAnswer, bool isApproved, object providerUserKey,
out MembershipCreateStatus status)
{
SqlConnection cnn = null;
SqlCommand cmd = null;
MembershipUser newUser = null;
try
{
cnn = new SqlConnection();
cnn.ConnectionString = "myConnectionString";
cnn.Open();
string insertQry = "Prepare the Insert query...";
cmd = new SqlCommand(insertQry, cnn);
cmd.ExecuteNonQuery();
// Right now I am giving default values for DateTime
// in Membership constructor.
newUser = new MembershipUser(
"AspNetCustomMembershipProvider",
username, null, String.Empty, String.Empty,
String.Empty, true, false, DateTime.Now,
DateTime.Now, DateTime.Now, DateTime.Now,
DateTime.Now
);
status = MembershipCreateStatus.Success;
}
catch (Exception ex)
{
status = MembershipCreateStatus.ProviderError;
newUser = null;
throw ex;
}
finally
cmd.Dispose();
cnn.Close();
}
return newUser;
}
// MembershipProvider Properties
public override string ApplicationName
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override bool EnablePasswordRetrieval
{
get { return false; }
}
public override bool EnablePasswordReset
{
get { return false; }
}
public override int MaxInvalidPasswordAttempts
{
get { throw new NotSupportedException(); }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { throw new NotSupportedException(); }
}
public override int MinRequiredPasswordLength
{
get { throw new NotSupportedException(); }
}
public override int PasswordAttemptWindow
{
get { throw new NotSupportedException(); }
}
public override MembershipPasswordFormat PasswordFormat
{
get { throw new NotSupportedException(); }
}
public override string PasswordStrengthRegularExpression
{
get { throw new NotSupportedException(); }
}
public override bool RequiresQuestionAndAnswer
{
get { return false; }
}
public override bool RequiresUniqueEmail
{
get { return false; }
}
public override MembershipUser GetUser(string username,
bool userIsOnline)
{
throw new NotSupportedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex,
int pageSize, out int totalRecords)
{
throw new NotSupportedException();
}
public override int GetNumberOfUsersOnline()
{
throw new NotSupportedException();
}
public override bool ChangePassword(string username,
string oldPassword, string newPassword)
{
throw new NotSupportedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username,
string password, string newPasswordQuestion,
string newPasswordAnswer)
{
throw new NotSupportedException();
}
public override bool DeleteUser(string username,
bool deleteAllRelatedData)
{
throw new NotSupportedException();
}
public override MembershipUserCollection
FindUsersByEmail(string emailToMatch, int pageIndex,
int pageSize, out int totalRecords)
{
throw new NotSupportedException();
}
public override MembershipUserCollection
FindUsersByName(string usernameToMatch, int pageIndex,
int pageSize, out int totalRecords)
{
throw new NotSupportedException();
}
public override string GetPassword(string username, string answer)
{
throw new NotSupportedException();
}
public override MembershipUser GetUser(object providerUserKey,
bool userIsOnline)
{
throw new NotSupportedException();
}
public override string GetUserNameByEmail(string email)
{
throw new NotSupportedException();
}
public override string ResetPassword(string username,
string answer)
{
throw new NotSupportedException();
}
public override bool UnlockUser(string userName)
{
throw new NotSupportedException();
}
public override void UpdateUser(MembershipUser user)
{
throw new NotSupportedException();
}
}
...the error I get is this:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 53: finally
Line 54: {
Line 55: cmd.Dispose();
Line 56: cnn.Close();
Line 57: }
Coding is indeed a nine-to-five job; nine pm to five am.
|