问题描述
你好,
这来自我的实验室手册练习9.2:
错误:找不到适合的方法来覆盖
A部分:
1.打开一个名为SecuredApplication
的新项目2.添加名为Users.cs
的新类文件3.添加以下代码行以创建支持反序列化的新类
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
{
//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.添加名为CustomMemberShipProvider.cs的新类文件.5.在CustomMemberShipProvider.cs文件中,添加以下指令:
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.在CustomMembershipProvider.cs文件中,继承CustomMembershipProvider类.
7.在同一文件中,将以下代码添加到CustomMembershipProvider类,以从xml获取用户:
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
{
// 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.在同一文件中,更改四种方法的实现:ValidateUser,CreateUser,ChangePassword和GetUser,
以及带有以下代码行的三个属性MinRequiredPasswordLength,RequiresQuestionAndAnswer和MinRequiredNonAlphanumericCharters:
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)
{
//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; }
}
这是我得到错误的地方,上述每个错误都有一个.蓝色下划线的单词是:ChangePassword,CreateUser,ValidateUser,GetUser,MinRequiredNonAlphanumericCharacters,MinRequiredPasswordLength,RequiresQuestionAndAnswer.
本书的下一步:
9.在同一文件中,创建一个继承自MembershipUser类的新类CustomMembershipProvider.您需要为此类创建一个构造函数,如下所示:
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
{
// 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)
{}
}
}
}
最后一步说:
10.在解决方案资源管理器中,双击web.config文件,并在< system.web>下添加以下xml行.节点.此代码会将此自定义提供程序设置为默认提供程序,并将限制匿名用户访问受保护的页面:
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>
至此,A部分结束.B部分继续创建登录",更改密码"和创建新的用户页面,如果没有上述错误,所有页面都将进行编译.
PS在B部分的末尾,将以下代码添加到web.config文件中,以允许匿名用户访问位于名为all的文件夹中的CreateUser.aspx页:
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.
推荐答案
class CustomMembershipProvider : MembershipProvider {
...
如果您只是键入它的内容而不真正理解代码在做什么,那么我不确定这种高级教程的价值.那是一个非常简单的错误,即编译器错误应该可以帮助您找到.
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.
这篇关于创建自定义会员资格提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!