How to Implement 2-Step Verification in ASP.NET MVC
page 7 of 12
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 70573/ 101

Trusted Clients

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);
        }
    }
}

View Entire Article

User Comments

Title: No source code?   
Name: Larry Q
Date: 2011-12-07 10:39:10 AM
Comment:
Hi everyone,

I enjoyed Keyvan's article very much, however the source code link doesn't work. Is it possible to get the source from another location? Many thanks for writing this article, before I forget.

-Larry






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 12:50:13 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search