[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceAuthentication : System.Web.Services.WebService
{
string xmlFilePath = string.Empty;
public WebServiceAuthentication()
{
xmlFilePath = Server.MapPath("data/users.xml");
}
[WebMethod]
public bool ChangePassword(string username, string newpassword)
{
XDocument users = XDocument.Load(xmlFilePath);
var user = from u in users.Elements("user")
where u.Element("username").Value.ToLower() == username.ToLower()
select u;
if (user != null && user.Count() > 0)
{
user.First().Element("password").Value = newpassword;
return true;
}
return false;
}
[WebMethod]
public bool ValidateUser(string userName, string passWord)
{
XDocument users = XDocument.Load(xmlFilePath);
var user = from u in users.Element("users").Elements("user")
where u.Element("username").Value.ToLower() == userName.ToLower() && u.Element("password").Value == passWord
select u;
if (user != null && user.Count() > 0)
return true;
return false;
}
[WebMethod]
public Customer CreateUser(string username, string password, string emailID)
{
XDocument users = XDocument.Load(xmlFilePath);
var user = from u in users.Elements("user")
where u.Attribute("email").Value.ToLower() == username.ToLower()
select u.Attribute("email").Value;
if (user.Count() <= 0)
throw new Exception("Email already in user");
else
{
users.Element("users").Add(new XElement("user",
new XAttribute("email", emailID),
new XElement("userName", username),
new XElement("password", password)));
users.Save(xmlFilePath);
}
return new Customer { UserName = username };
}
[WebMethod]
public string ResetPassword(string username)
{
string newPass = string.Empty;
XDocument users = XDocument.Load(xmlFilePath);
var user = from u in users.Elements("user")
where u.Element("username").Value.ToLower() == username.ToLower()
select u;
if (user != null && user.Count() > 0)
{
newPass = Guid.NewGuid().ToString().Substring(0, 10);
user.First().Element("password").Value = newPass;
}
else
throw new Exception("User not found.");
return newPass;
}
[WebMethod]
public bool UpdateCustomer(Customer customerToBeUdpated)
{
XDocument users = XDocument.Load(xmlFilePath);
var userToBeUpdated = (from u in users.Elements("user")
where u.Attribute("email").Value.ToLower() == customerToBeUdpated.UserName.ToLower()
select u);
if (userToBeUpdated != null && userToBeUpdated.Count() > 0)
{
XElement user = userToBeUpdated.First();
user.Element("password").Value = customerToBeUdpated.Password;
if (user.Element("firstname") != null)
user.Element("firstname").Value = customerToBeUdpated.FirstName;
else
user.Add(new XElement("firstname", customerToBeUdpated.FirstName));
if (user.Element("lastname") != null)
user.Element("lastname").Value = customerToBeUdpated.LastName;
else
user.Add(new XElement("lastname", customerToBeUdpated.LastName));
if (user.Element("address") != null)
{
user.Element("address").Element("street").Value = customerToBeUdpated.Address.Street;
user.Element("address").Element("city").Value = customerToBeUdpated.Address.City;
user.Element("address").Element("state").Value = customerToBeUdpated.Address.State;
user.Element("address").Element("country").Value = customerToBeUdpated.Address.Country;
}
else
{
user.Add(new XElement("address", new XElement("street", customerToBeUdpated.Address.Street),
new XElement("city", customerToBeUdpated.Address.City),
new XElement("state", customerToBeUdpated.Address.State),
new XElement("country", customerToBeUdpated.Address.Country)));
}
return true;
}
return false;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<users>
<user email="a@a.com">
<username>sandeep</username>
<password>pass</password>
<firstname>Sandeep</firstname>
<lastname>P.R</lastname>
<address>
<street>Blah blah</street>
<city>City</city>
<state>State</state>
<country>Country</country>
</address>
</user>
<user email="sndppr@gmail.com">
<username>sndppr@gmail.com</username>
<password>pass</password>
<firstname>Sandeep</firstname>
<lastname>P.R</lastname>
<address>
<street>Blah blah</street>
<city>City</city>
<state>State</state>
<country>Country</country>
</address>
</user>
</users>
public class Customer : System.Web.Security.MembershipUser
{
public Customer()
{
}
private string userName = string.Empty;
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string EMail { get; set; }
public string Password { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
The method or operation is not implemented.Now let's see the code where I have extended the System.Web.Security.Member
public class CustomMembershipProvider : System.Web.Security.MembershipProvider
{
public CustomMembershipProvider() : base()
{
}
public override string ApplicationName
{
get
{
return "Authentication";
}
set
{
throw new NotImplementedException();
}
}
//Non implemented methods have been removed for brevity.
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
WebServiceAuthentication wsa = new WebServiceAuthentication();
return wsa.ChangePassword(username, newPassword);
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
MembershipUser cust = this.GetUser(username, false);
if (cust == null)
{
WebServiceAuthentication wsa = new WebServiceAuthentication();
cust = wsa.CreateUser(username, password, email);
status = cust != null ? MembershipCreateStatus.Success : MembershipCreateStatus.UserRejected;
}
else
status = MembershipCreateStatus.DuplicateUserName;
return cust;
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
WebServiceAuthentication wsa = new WebServiceAuthentication();
MembershipUser mu = wsa.GetUser(username);
return mu;
}
public override int MinRequiredNonAlphanumericCharacters
{
get { return 0; }
}
public override int MinRequiredPasswordLength
{
get { return 2; }
}
public override bool RequiresQuestionAndAnswer
{
get { return false; }
}
public override bool RequiresUniqueEmail
{
get { return false; }
}
//Non implemented methods have been removed for brevity
public override void UpdateUser(MembershipUser user)
{
WebServiceAuthentication wsa = new WebServiceAuthentication();
wsa.UpdateCustomer(user);
}
public override bool ValidateUser(string username, string password)
{
WebServiceAuthentication wsa = new WebServiceAuthentication();
if (wsa.ValidateUser(username, password))
{
FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), true, string.Empty);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat));
HttpContext.Current.Request.Cookies.Add(authCookie);
return true;
}
else
{
return false;
}
}
}
<membership defaultProvider="CustomProvider" >
<providers>
<add name="CustomProvider" type="CustomMembershipProvider" />
</providers>
</membership>
<connectionStrings>
<add name="connStr" connectionString="Data Source=testserver;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa"/>
</connectionStrings>
<!--Membership provider configuration with membership properties set in web.config. The connectionStringName should match with the one provided in you web.config.-->
<membership defaultProvider="CustomProvider" >
<providers>
<add name="CustomProvider" type="CustomMembershipProvider" applicationName="TestApp" connectionStringName="connStr" enablePasswordRetrieval="false" enablePasswordReset="true" maxInvalidPasswordAttempts="5" />
</providers>
</membership>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Nice work!
Author
Commented: