Web Service authentication

Source: Internet
Author: User

1. We implement a class for identity authentication, with the file name MySoapHeader. cs

The MySoapHeader class inherits from System. Web. Services. Protocols. SoapHeader. Two member variables, UserName and PassWord, are defined, and a user-authenticated function ValideUser is defined. It provides the UserName and PassWord check function.

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. Services;
Using System. Web. Services. Protocols;
/// <Summary>
/// Summary of MySoapHeader
/// </Summary>
Public class MySoapHeader: SoapHeader
{
Public MySoapHeader ()
{
//
// TODO: add the constructor logic here
//
}
Public string UserName;
Public string PassWord;
Public bool ValideUser (string in_UserName, string in_PassWord)
{
If (in_UserName = "zxq") & (in_PassWord = "123456 "))
{
Return true;
}
Else
{
Return false;
}
}
}

 

2. The following Code creates WebService. asmx WebService. cs:

Using System;
Using System. Collections;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;

/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class WebService: System. Web. Services. WebService
{

Public WebService ()
{

// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
Public MySoapHeader header; // defines the user authentication class variable header
[WebMethod (Description = "user verification test")]
[System. Web. Services. Protocols. SoapHeader ("header")] // soap header for User Authentication
Public string HelloWorld (string contents)
{
// Verify the access permission
If (header. ValideUser (header. UserName, header. PassWord ))
{
Return contents + "executed ";
}
Else
{
Return "You are not authorized to access ";
}
}
}

 

3. Create a Default. aspx Client

Default. aspx. cs code

Using System;
Using System. Configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Com. cn1yw. WebService test = new com. cn1yw. WebService (); // web reference (changed to your own)
Com. cn1yw. MySoapHeader Header = new com. cn1yw. MySoapHeader (); // create a soap Header object by referencing the web (change it to your own)
// Set the soap header variable
Header. UserName = "zxq ";
Header. PassWord = "123456 ";
Test. MySoapHeaderValue = Header;
// Call the web method
Response. Write (test. HelloWorld ("I am strong "));
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.