Click here to Skip to main content
15,946,158 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Xml.Serialization;

namespace SecuredApplication
{

    //These classes are used to deserialize userdata.Xml into object.
    [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:

C#
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:

MSIL
public class CustomMembershipProvider
    {
        // Declares Users variable at class level
        private Users users;

        // Constructer for the class
        public CustomMembershipProvider()
         {
        // Get all the users from the userdata.Xml and deserialize in the user s object
        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()
        {
            // Serialize and write the current users object in the Userdata.xml file.
            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:

C++
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)
        {
            //CustomMembershipUser is a user defined class inheriting MemberShip user class.
            //The definition is given in the next step.
            return new CustomMembershipUser(username, "") as MembershipUser;

        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            // This is to set contraints on the password.
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            // This is to set contraints on the password.
            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:


C#
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:

C++
namespace SecuredApplication
{


    public class CustomMembershipProvider
    {
        // Declares Users variable at class level
        private Users users;

        // Constructer for the class
        public CustomMembershipProvider()
         {
        // Get all the users from the userdata.Xml and deserialize in the user s object
        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()
        {
            // Serialize and write the current users object in the Userdata.xml file.
            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)
        {
            //CustomMembershipUser is a user defined class inheriting MemberShip user class.
            //The definition is given in the next step.
            return new CustomMembershipUser(username, "") as MembershipUser;

        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            // This is to set contraints on the password.
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            // This is to set contraints on the password.
            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:

XML
<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:

XML
<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.
Posted

"6. In the CustomMembershipProvider.cs file, inherit the CustomMembershipProvider class."
You didn't do this. CustomMembershipProvider doesn't inherit from anything. You want
class CustomMembershipProvider : MembershipProvider {
 ...


Edit: I'm not sure of the value of such high level tutorials, if you are simply typing in what it says and not really understanding what the code is doing. That's a pretty simple mistake that the compiler error should have been able to help you find.
 
Share this answer
 
v2
Comments
censurl 1-Aug-12 13:35pm    
You were right. Found the answer I was looking for on codeproject:

Hint: To add all the methods to be implemented, place your cursor at the beginning (or end) of the word MembershipProvider, press Ctrl + . and then choose Implement abstract class 'MembershipProvider'.
Please check this link[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900