DevTrain Startseite Advanced Developers Conference vom 14.-15. Februar 2011  
  
  
SUCHEN:  
ARTIKEL ONLINE: 525   

Kategorien
.NET
Datenbanken
Web
XML

Allgemein
Camp
Foren
Events
Persönliche Einstellungen
Registrieren
Prämien Shop
Kontakt
Impressum
Über DevTrain

Autoren



 

Forum: ASP.NET2.0 | Thema: AW: MembershipProvider mit XML | Von: Thomas Hauser ( 30.07.2007 23:56)

Hallo,

also die Frage ist, wie die Daten in die XML-Datei geschrieben wurden. Ich nehme für sowas gerne den XMLSerializer, weil ich damit echt wenig Arbeit habe. ;-)

Allerdings gebe ich hier noch keine Garantie oder sowas. Habs mal getestet. In meinen Versuchen hat es funktioniert. Die XML-Datei wird in App_Code/membership.xml abgelegt.

Thomas Hauser
http://thomashauser.spaces.live.com

Mein Aufbau: 3 Klassen:
- Member (erbt von MembershipUser, Eine Erweiterung war notwendig, damit auch das Kennwort und solche Dinge serialisiert werden)
- Membership (Die Sammlung von Membern sozusagen, ein Dictionary, das die entsprechenden User enthält)
- XmlMembershipProvider (Die Implementierung der abstrakten MembershipProvider-Klasse. Diese ruft hauptsächlich nur noch Methoden in der Membership Klasse auf)

Hier der Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;

[XmlRoot("Member")]
public class Member: MembershipUser
{
private string username;
private string password;
private string question;
private string answer;

[XmlElement("Approved")]
public bool Approved
{
get { return base.IsApproved; }
set { base.IsApproved = value; }
}

[XmlElement("PasswordAnswer")]
public string Answer
{
get { return answer; }
set { answer = value; }
}

[XmlElement("PasswordQuestion")]
public string Question
{
get { return question; }
set { question = value; }
}

[XmlIgnore()]
public override string PasswordQuestion
{
get { return question; }
}

[XmlElement("Password")]
public string Password
{
get { return password; }
set { password = value; }
}

[XmlElement("Username")]
public string Username
{
get { return username; }
set { username = value; }
}

public override string UserName
{
get { return username; }
}

public Member()
{
}

public Member(string providername, string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey)
:base(providername, username, providerUserKey, email, passwordQuestion, "", isApproved,
false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now)
{
this.username = username;
this.password = password;
this.question = passwordQuestion;
this.answer = passwordAnswer;
}
}

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.IO;

[XmlRoot("Membership")]
public class Membership
{
private string applicationName;
private Dictionary<string, Member> members;

[XmlArray("Members")]
public List<Member> Members
{
get { return new List<Member>(members.Values); }
set
{
members = new Dictionary<string, Member>();
foreach (Member member in value)
{
members.Add(member.UserName, member);
}
}
}

[XmlElement("ApplicationName")]
public string ApplicationName
{
get { return applicationName; }
set { applicationName = value; }
}

public Membership()
{
members = new Dictionary<string, Member>();
}

public bool ChangePassword(string username, string oldPassword, string newPassword)
{
if (!members.ContainsKey(username))
throw new ApplicationException("Benutzer existiert nicht");
return members[username].ChangePassword(oldPassword, newPassword);
}

public bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
if (!members.ContainsKey(username))
throw new ApplicationException("Benutzer existiert nicht");
return members[username].ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer);
}

public void CreateUser(string providername, string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
if (members.ContainsKey(username))
status = MembershipCreateStatus.InvalidUserName;
else
{
members.Add(username, new Member(providername, username, password, email,
passwordQuestion, passwordAnswer, isApproved, providerUserKey));
status = MembershipCreateStatus.Success;
}
}

public void UpdateUser(MembershipUser user)
{
if (members.ContainsKey(user.UserName))
{
members[user.UserName].Password = user.GetPassword();
members[user.UserName].Username = user.UserName;
members[user.UserName].Email = user.Email;
}
}

public bool ValidateUser(string username, string password)
{
if (members.ContainsKey(username))
return (members[username].GetPassword() == password);
else
return false;
}

public bool DeleteUser(string username, bool deleteAllRelatedData)
{
if (members.ContainsKey(username))
return members.Remove(username);
else
return false;
}

public MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
MembershipUserCollection col = new MembershipUserCollection();
List<MembershipUser> users = new List<MembershipUser>();
foreach (MembershipUser user in members.Values)
{
if (user.Email.ToLower() == emailToMatch.ToLower())
users.Add(user);
}
totalRecords = users.Count;
for (int i = pageIndex * pageSize; i < ((pageIndex + 1) * pageSize) || i < totalRecords; i++)
{
col.Add(users[i]);
}
return col;
}

public MembershipUser GetUser(string username)
{
if (members.ContainsKey(username))
return members[username];
else
return null;
}

public MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
MembershipUserCollection col = new MembershipUserCollection();
List<MembershipUser> users = new List<MembershipUser>();
foreach (MembershipUser user in members.Values)
{
users.Add(user);
}
totalRecords = users.Count;
for (int i = pageIndex * pageSize; i < ((pageIndex + 1) * pageSize) || i < totalRecords; i++)
{
col.Add(users[i]);
}
return col;
}

public int GetNumberOfUsersOnline()
{
int online = 0;
foreach (MembershipUser user in members.Values)
{
if (user.IsOnline)
online++;
}
return online;
}

public string GetPassword(string username, string answer)
{
if (members.ContainsKey(username))
return members[username].Password;
else
return null;
}

public MembershipUser GetUserByProviderKey(object providerUserKey)
{
foreach (MembershipUser user in members.Values)
{
if (user.ProviderUserKey.Equals(providerUserKey))
return user;
}
return null;
}

public string GetUsernameByEmail(string email)
{
foreach (MembershipUser user in members.Values)
{
if (user.Email.ToLower() == email.ToLower())
return user.UserName;
}
return null;
}

public string ResetPassword(string username, string answer)
{
if (members.ContainsKey(username))
return members[username].ResetPassword();
else
return null;
}

public bool UnlockUser(string username)
{
if (members.ContainsKey(username))
return members[username].UnlockUser();
else
return false;
}

}

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Web.Configuration;

public class XmlMembershipProvider : MembershipProvider
{
private string xmlFilename;
private Membership members;

public string XmlFilename
{
get { return xmlFilename; }
set { xmlFilename = value; Load(); }
}

public override string ApplicationName
{
get { return members.ApplicationName; }
set { members.ApplicationName = value; }
}

public XmlMembershipProvider()
: base()
{
Load();
}

~XmlMembershipProvider()
{
Save();
}

public void Load()
{
if (string.IsNullOrEmpty(this.xmlFilename) || !File.Exists(this.xmlFilename))
this.xmlFilename = HttpContext.Current.Server.MapPath("~/App_Data/membership.xml");
if (File.Exists(this.xmlFilename))
{
using (FileStream fs = new FileStream(this.xmlFilename, FileMode.Open, FileAccess.Read))
{
XmlSerializer xmls = new XmlSerializer(typeof(Membership));
members = (Membership)xmls.Deserialize(fs);
fs.Close();
}
}
else
{
members = new Membership();
}
}

public void Save()
{
if (string.IsNullOrEmpty(this.xmlFilename))
this.xmlFilename = HttpContext.Current.Server.MapPath("~/App_Data/membership.xml");
using (FileStream fs = new FileStream(xmlFilename, FileMode.OpenOrCreate, FileAccess.Write))
{
XmlSerializer xmls = new XmlSerializer(typeof(Membership));
xmls.Serialize(fs, members);
fs.Close();
}
}

public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
if (members.ChangePassword(username, oldPassword, newPassword))
{
Save();
return true;
}
else
return false;
}

public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
if (members.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer))
{
Save();
return true;
}
else
return false;
}

public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
members.CreateUser(base.Name, username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
Save();
return members.GetUser(username);
}

public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
bool deleted = members.DeleteUser(username, deleteAllRelatedData);
if (deleted)
Save();
return deleted;
}

public override bool EnablePasswordReset
{
get { return true; }
}

public override bool EnablePasswordRetrieval
{
get { return false; }
}

public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
return members.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
}

public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
MembershipUserCollection col = new MembershipUserCollection();
col.Add(members.GetUser(usernameToMatch));
totalRecords = 1;
return col;
}

public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
return members.GetAllUsers(pageIndex, pageSize, out totalRecords);
}

public override int GetNumberOfUsersOnline()
{
return members.GetNumberOfUsersOnline();
}

public override string GetPassword(string username, string answer)
{
return members.GetPassword(username, answer);
}

public override MembershipUser GetUser(string username, bool userIsOnline)
{
return members.GetUser(username);
}

public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
return members.GetUserByProviderKey(providerUserKey);
}

public override string GetUserNameByEmail(string email)
{
return members.GetUsernameByEmail(email);
}

public override int MaxInvalidPasswordAttempts
{
get { return 5; }
}

public override int MinRequiredNonAlphanumericCharacters
{
get { return 0; }
}

public override int MinRequiredPasswordLength
{
get { return 5; }
}

public override int PasswordAttemptWindow
{
get { return 20; }
}

public override MembershipPasswordFormat PasswordFormat
{
get { return MembershipPasswordFormat.Hashed; }
}

public override string PasswordStrengthRegularExpression
{
get { return @"\w{5,}"; }
}

public override bool RequiresQuestionAndAnswer
{
get { return false; }
}

public override bool RequiresUniqueEmail
{
get { return false; }
}

public override string ResetPassword(string username, string answer)
{
string pwd = members.ResetPassword(username, answer);
Save();
return pwd;
}

public override bool UnlockUser(string userName)
{
return members.UnlockUser(userName);
}

public override void UpdateUser(MembershipUser user)
{
members.UpdateUser(user);
}

public override bool ValidateUser(string username, string password)
{
return members.ValidateUser(username, password);
}
}


Thomas Hauser (Trainer und Consultant, Blog: http://thomashauser.spaces.live.com, Forum: http://www.sharepointbook.de - Die Forum-Webseite zum Buch!)

Betreff Von Datum
AW: AW: MembershipProvider mit XML
Da gibts doch auch was auf codeplex.com:<br><br>XML Membership Provider auf Codeplex:<br>http://www.codeplex.com/aspnetxmlproviders
Thomas Hauser 13.08.2007 23:50

Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!

 Betreff:
 Nachricht: Den Beitrag finden Sie nun unter: http://beta.devtrain.de/foren Die Benutzerdaten und Foreninhalte von beta.devtrain.de und www.devtrain.de sind die selben.
Sie können sich dort sogar per RSS über neue Inhalte informieren lassen.
Bei Problemen bitte direkt Mail an asp [AT] ppedv.de.

 Signatur:

  



Login
Username:


Passwort:






Passwort vergessen?

Advanced Developers Conference vom 14.-15. Februar 2011

© Copyright 2003 ppedv AG