Not only we need to manage users and store/retrieve their
verification codes, but also we need a mechanism to store trusted clients and
retrieve them to/from data store. Again, I use a simple XML-based data store
and write LINQ to XML code to store a trusted client for a particular username
or retrieve an existing item (Listing 5).
Listing 5: Trusted clients
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace AspNetPhoneVerificationSample.Models
{
public static class TrustedClients
{
public static bool ValidateClient(string username, string ip,
string useragent)
{
string path = HttpContext.Current.
Server.MapPath("~/App_Data/trustedclients.xml");
XDocument doc = XDocument.Load(path);
var client = (from c in doc.Element("Clients").Descendants("Client")
where
((c.Attribute("username").Value == username) &&
(c.Attribute("ip").Value == ip) &&
(c.Attribute("useragent").Value == useragent))
select c).SingleOrDefault();
if (client != null)
return true;
return false;
}
public static void AddClient(string username, string ip, string useragent)
{
string path = HttpContext.Current.
Server.MapPath("~/App_Data/trustedclients.xml");
XDocument doc = XDocument.Load(path);
XElement newClient = new XElement("Client");
newClient.SetAttributeValue("username", username);
newClient.SetAttributeValue("ip", ip);
newClient.SetAttributeValue("useragent", useragent);
doc.Element("Clients").Add(newClient);
doc.Save(path);
}
}
}