Monday, October 29, 2012

Authenticate .NET Web Service with Custom SOAP Header in Sql Server

Authenticate .NET Web Service with Custom SOAP Header in Sql Server 

First step open Web Services 

Basically all the client needs to do is create an authentication object, fill out the username and password, then pass them to the web service object. The web service code is also pretty simple, the .NET framework lets you create custom SOAP headers by deriving from the SoapHeader class, so we wanted to add a username and password:


and create Authenticate Program .create and UserMaster Table 

Create Table UserMaster(UserId int identity ket(1,1),UserName varchar(20),UserPwd varchar(50))

then after we can create a web services in asp.net add + web services 


using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
    public MyHeader consumer;
    public SqlConnection con = null;
    public SqlCommand cmd = null;
    
    public Service () 
    {

        con = new SqlConnection("server=10.21.157.35;database=elekha;uid=elekha;pwd=iamdoingaccounts;");

    }

    [WebMethod]
    [SoapHeader("consumer", Required = true)]
    public string GetBalance() 
    {
        if (checkConsumer())
            return consumer.UserName + " had 9999999999999 credit";
        else
            return "Error in authentication";
    }
    private bool checkConsumer()
    {
        try
        {
            // In this method you can check the username and password 
            // with your database or something
            // You could also encrypt the password for more security
            
                con.Open();
            

            cmd = new SqlCommand("select * from UserMaster where UserName='"+consumer.UserName+"' and UserPwd='"+consumer.Password+"'", con);
            SqlDataReader rd = cmd.ExecuteReader();
            if (consumer != null)
            {
                if (rd.Read())
                {
                    return true;
                    
                }
                else return false;
            }
            else { return false; }
            con.Close();
            //if (consumer != null)
            //{
            //    if (consumer.UserName == "virendra" && consumer.Password == "1234")
            //        return true;
            //    else
            //        return false;
            //}
            //else
            //    return false;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public class MyHeader : SoapHeader
    {
        public string UserName;
        public string Password;
    }
}
==========================================================
I should also mention that when I say SOAP headers, I actually mean the soap:Header element in a SOAP request, it has nothing to do with the HTTP headers sent with the request. The SOAP request looks something like:
then we can call the Web services in front end 

or .aspx Page


<asp:Panel ID="Panel1" runat="server" BackColor="#CCFFCC" Height="200px" 
        Width="300px">
        <br />
        <br />
        User_Name ::
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        Password&nbsp; ::&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" BackColor="#99CCFF" 
            onclick="Button1_Click" Text="Button" />
    </asp:Panel>

We just get reference to the service and the SOAP header, assign the SOAP header properties, attach it with the SOAP message and then make our call to the web method.
then code behind page Or aspx.cs Page


protected void Button1_Click(object sender, EventArgs e)
    {
        MyWebServices.Service ws = new MyWebServices.Service();
        MyWebServices.MyHeader sh = new MyWebServices.MyHeader();
        sh.UserName = TextBox1.Text.ToString();
        sh.Password = TextBox2.Text.ToString();
        ws.MyHeaderValue = sh;
        Response.Write("<script>alert('"+ws.GetBalance()+"')</script>");
    }



=====================================


No comments:

Post a Comment