Implementing authentication using SOAP headers for ASP.NET web services.

by Tom Miller 11. January 2010 00:16

SOAP headers are a perfect vehicle for performing authentication for web services.  By using SOAP headers we are able to pass our authentication details prior to any method calls.  This is a much better solution than passing authentication details for each and every method call.

To implement SOAP headers we must first create a class to hold our authentication details.  This class must derive from the System.Web.Service.Protocols.SoapHeader class.  It must also expose two string fields called userName and password.

We must then declare a public type of this class within our web service class.  Call this AuthenticationHeader.

The above has given the ability for a user to create an instance of our soap header class.  Set the credentials (using the userName and password fields) and set the AuthenticationHeader field/property of the web service.

We must then implement an internal function to perform validation of credentials.  I've simply hardcoded the example below.  We then must decorate our public methods with the [SoapHeader] attribute and set the member name to AuthenticationHeader as well as the IsRequired property to true.

For a basic example of this implementation please see the c# code below:-

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebServiceAuthentication
{
    /// <summary>
    /// Service to demonstrate authentication
    /// with SOAP headers.
    /// </summary>
    [WebService(Namespace = "http://bluweb.co.uk/WebServiceAuthentication")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, 
    // using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebServiceAuthentication : System.Web.Services.WebService
    {
        [SoapHeader("AuthenticationHeader", Required=true)]
        [WebMethod(Description=@"Demonstrates authentication using 
        SOAP headers for ASP.NET web services")]
        public string TestAuthentication()
        {
            if (!IsAuthenticated())
                throw new 
                    InvalidOperationException("Invalid credentials.");

            return "You're authenticated";
        }

        private bool IsAuthenticated()
        {
            if (AuthenticationHeader != null)
            {
                if (AuthenticationHeader.userName == 
                    "username" && AuthenticationHeader.password == "password")
                    return true;
            }
            return false;
        }

        public AuthHeader AuthenticationHeader;
    }

    public class AuthHeader : SoapHeader
    {
        public string userName;
        public string password;
    }
}

Tags:

ASP.NET | C#

Share Your Website Design Prices.

by Tom Miller 6. December 2009 11:34

The Web Design Blog is gathering statistics on web design pricing in an effort to produce some guidelines on how much we should be charging for our services.

http://www.thewebdesignblog.co.uk/web-resources/share-your-web-design-prices-anonymously/

Tags:

Website Design

Basic Website Design Tips for Beginners

by Tom Miller 5. December 2009 00:36

Your websites should speak a lot about yourself and the business you represent. How things are shown online can influence how visitors feel about your company, and whether or not they would like to buy your products or hire your service. First of all, what aspects of a web site are considered to be visitor-friendly?

 - It must be an easy site to navigate. Every link on your site must work, and you should provide buttons above, below, and on the side of every page so that visitors can easily go to and from portions of your site.

 - It must be pleasing to look at. No flashy colors, blinking texts, or excessive animations. The site should also not be too plain that browsing it would be boring.

 - It must have an impact. It doesn’t mean that fancy stuff is needed. The message of the company must be presented clearly and with conviction. A site must appear as professional as possible to elicit the trust of the potential client.

How to design a website: stuff you need first

There are various facets of website design that you can take advantage of, but before anything else, you must start off on the right foot. Here are two things that you must have before you set yourself on planning the blueprints of your website:

1. Do you have a domain name? You must decide on one that can easily be remembered, preferably a name that is keyword rich. Do not be vague, and ensure that it represents your site and your business very well. Many sources say, avoid using hyphens (-) in your domain name, and stick to three words or less in a name. Note that a .com site or .net site is better than .info or .org.

2. Have you found a web host? A web host provides you with your drawing board to place your website in. In a way, they give you the space on the World Wide Web to upload your site and all its contents. Web hosts may be free or paid, and you are given tools and tech support to help you out.

When you have nailed these two down, you can then plan your website. You can design it yourself if you are familiar with HTML and website design software. You may also use templates which are available online, for you to have a professional looking color scheme and lay out.

If you can’t do it yourself, what are your options?

Of course, not all people have the time to familiarize themselves with HTML, Flash, Macromedia Dreamweaver, and other programs. If you don’t have the skills nor the time to design your website yourself, you can seek the help of the professionals. There are many website design firms across the globe that can bring to life your ideas for website design, so you spend less time worrying, and more time focusing on other aspects of your business.

Tags:

Website Design

Dynamic ORDER BY clause in T-SQL

by Tom Miller 1. December 2009 18:18

This is something that came in handy when sorting reports, in SQL Reporting Services (SSRS), using parameters.

Unfortunately SSRS doesn't have the facility to sort a table of data by selecting a column header.  One work around for this is to add a Sort parameter.  The parameter would have options for each sortable column header and the value should return the actual column to sort in the database.  Given this your stored procedure would look something like this:

CREATE PROC sp_CustomerReport
    @SortField VARCHAR(20)
AS

    SELECT
        Customer.CustomerName,
        Customer.CustomerAddressLine1,
        Customer.CustomerAddressLine2,
        Customer.City,
        Customer.Postcode
    FROM
        Customer WITH(NOLOCK)
    ORDER BY
        CASE
            WHEN    @SortField = 'CustomerName'
                THEN    Customer.CustomerName
            WHEN    @SortField = 'City'    
                THEN    Customer.City
            ELSE
                Customer.CustomerName
        END
GO

Tags: ,

T-SQL

How to hide the root node of a TreeView control in ASP.NET using XML.

by Tom Miller 23. November 2009 20:26

So, you're binding data to the TreeView control in ASP.NET using the XmlDataSource class and you need to hide the root node/element.  Firstly you probably looked through the vast array of properties exposed by the TreeView control and not found the property you were after.  Since an XML document must have a root element it's not as if you can omit this from the XML document itself.

The trick is to set the XPath property of the XmlDataSource class instance.  In my actual code I return XML from SQL Server using the "FOR XML AS AUTO" statement in T-SQL and create an XmlDocument based on that.  You may or may not be doing it this way but the example below, for demonstration purposes only, loads an imaginary XML document from the App_Data folder.

CODE
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/xmlfile.xml"));

            XmlDataSource ds = new XmlDataSource();
            ds.Data = doc.InnerXml;
            // To remove the root node set the XPath property.
            ds.XPath = "/rootnode/node";

            TreeView1.DataSource = ds;
            TreeView1.DataBind();
        }

Tags: , , ,

ASP.NET | C#