Hello,
This comes from my Lab Manual Exercise 9.2:
The Error :no suitable method found to override
PART A:
1. Open new project called SecuredApplication
2. Add new class file called Users.cs
3. Add the following lines of code to create new classes to support deserialization
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
namespace SecuredApplication
{
[XmlRoot]
[Serializable]
public class Users
{
[XmlElement(ElementName = "User")]
public List<User> users;
}
[Serializable]
public class User
{
[XmlAttribute(AttributeName = "username")]
public string username { get; set; }
[XmlAttribute(AttributeName = "password")]
public string password { get; set; }
[XmlAttribute(AttributeName = "email")]
public string email { get; set; }
public User(string username, string password, string email)
{
this.username = username;
this.password = password;
this.email = email;
}
public User() { }
}
}
4. Add new class file called CustomMemberShipProvider.cs
5. In the CustomMemberShipProvider.cs file add the following directives:
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.Web.Caching;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
6. In the CustomMembershipProvider.cs file, inherit the CustomMembershipProvider class.
7. In the same file, add the following code to the CustomMembershipProvider class to get the users from the xml:
public class CustomMembershipProvider
{
private Users users
public CustomMembershipProvider()
{
users = new Users()
XmlSerializer x = new XmlSerializer(users.GetType())
if (!File.Exists(HttpContext.Current.Server.MapPath("~/UserData.xml")))
{
StreamWriter sw = File.CreateText(HttpContext.Current.Server.MapPath("~/UserData.xml"))
sw.WriteLine ("<Users><Users>")
sw.Close()
}
FileStream fs = File.OpenRead(HttpContext.Current.Server.MapPath("~/UserData.xml"))
users = (Users)x.Deserialize(fs)
fs.Close()
}
private void UpdateUserDataXML()
{
XmlSerializer x = new XmlSerializer(users.GetType())
XmlTextWriter xtw = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/userdata.xml"), System.Text.Encoding.UTF8)
x.Serialize(xtw, users)
xtw.Close()
}
8. In the same file, change the implementation of the four methods: ValidateUser, CreateUser, ChangePassword, and GetUser,
and the three properties MinRequiredPasswordLength, RequiresQuestionAndAnswer, and MinRequiredNonAlphanumericCharters with the following lines of code:
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
User user;
foreach (User usr in users.users)
if (usr.username.Equals(username, StringComparison.OrdinalIgnoreCase) && usr.password == oldPassword)
{
user = usr;
user.password = newPassword;
UpdateUserDataXML();
return true;
}
return false;
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
User user = new User(username, password, email);
users.users.Add(user);
UpdateUserDataXML();
status = MembershipCreateStatus.Success;
return null;
}
public override bool ValidateUser(string username, string password)
{
foreach (User usr in users.users)
if (usr.username.Equals(username, StringComparison.OrdinalIgnoreCase) && usr.password == password)
return true;
return false;
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
return new CustomMembershipUser(username, "") as MembershipUser;
}
public override int MinRequiredNonAlphanumericCharacters
{
get { return 0; }
}
public override int MinRequiredPasswordLength
{
get { return 3; }
}
public override bool RequiresQuestionAndAnswer
{
get { return false; }
}
This is where I am getting the error, one for each of the above. The words underline in blue are: ChangePassword, CreateUser, ValidateUser, GetUser, MinRequiredNonAlphanumericCharacters, MinRequiredPasswordLength, RequiresQuestionAndAnswer.
The next step in the book:
9. In the same file create a new class CustomMembershipProvider inheriting from MembershipUser class. you need to create a constructor for this class as given:
public class CustomMembershipUser : MembershipUser
{
public CustomMembershipUser(string username, string email)
: base("myProvider", username, "",email, "","",true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now)
{}
}
The entire page:
namespace SecuredApplication
{
public class CustomMembershipProvider
{
private Users users;
public CustomMembershipProvider()
{
users = new Users();
XmlSerializer x = new XmlSerializer(users.GetType());
if (!File.Exists(HttpContext.Current.Server.MapPath("~/UserData.xml")))
{
StreamWriter sw = File.CreateText(HttpContext.Current.Server.MapPath("~/UserData.xml"));
sw.WriteLine ("<Users><Users>");
sw.Close();
}
FileStream fs = File.OpenRead(HttpContext.Current.Server.MapPath("~/UserData.xml"));
users = (Users)x.Deserialize(fs);
fs.Close();
}
private void UpdateUserDataXML()
{
XmlSerializer x = new XmlSerializer(users.GetType());
XmlTextWriter xtw = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/userdata.xml"), System.Text.Encoding.UTF8);
x.Serialize(xtw, users);
xtw.Close();
}
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
User user;
foreach (User usr in users.users)
if (usr.username.Equals(username, StringComparison.OrdinalIgnoreCase) && usr.password == oldPassword)
{
user = usr;
user.password = newPassword;
UpdateUserDataXML();
return true;
}
return false;
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
User user = new User(username, password, email);
users.users.Add(user);
UpdateUserDataXML();
status = MembershipCreateStatus.Success;
return null;
}
public override bool ValidateUser(string username, string password)
{
foreach (User usr in users.users)
if (usr.username.Equals(username, StringComparison.OrdinalIgnoreCase) && usr.password == password)
return true;
return false;
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
return new CustomMembershipUser(username, "") as MembershipUser;
}
public override int MinRequiredNonAlphanumericCharacters
{
get { return 0; }
}
public override int MinRequiredPasswordLength
{
get { return 3; }
}
public override bool RequiresQuestionAndAnswer
{
get { return false; }
}
public class CustomMembershipUser : MembershipUser
{
public CustomMembershipUser(string username, string email)
: base("myProvider", username, "",email, "","",true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now)
{}
}
}
}
The last step says:
10. In the solution explorer ,double click the web.config file and add the following lines of xml under the <system.web> node. This code will set this custom provider as the default provider and will restrict anonymous users accessing the secured pages:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="Default.aspx">
</forms>
</authentication>
<membership defaultProvider="myProvider">
<providers>
<add name="myProvider" type="Authentication.CustomMembershipProvider"/>
</providers>
</membership>
<authorization>
<deny users="?"/>
</authorization>
That is the end of Part A. Part B goes on to create the Log on, Change Password, and create new user Pages all of which would compile if not for the errors above.
PS at the end of part B the following code was added to the web.config file to allow anonymous users access to the CreateUser.aspx page located in a folder named all:
<location path="all">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<system.web>
I hope I have given enough information in order to solve this problem, if there is anything else I need to provide please let me know.
Thanks in advance.