Use ASP. NET forms to implement WebService Authentication

Source: Internet
Author: User
ASP. NET with low security requirements Program . If we compare the two, it is clear that forms-based authentication is more convenient and easy to use. Can we apply Forms authentication to WebService?
Theoretically, it is feasible to use forms-based authentication for WebService. However, the following two problems may occur during use:
1. Forms-based authentication is also a cookie-based authentication method. When using a browser, this issue does not need to be considered. However, for applications that use WebService, Cookies cannot be saved by default. We need to do this ourselves.
2. WebService Since it is A 2a (Application to Application) It is obviously not suitable for applications to use web forms for identity authentication. Moreover, this will inevitably lead to human-computer interaction and greatly compromise WebService applications.
Next, we will solve these two problems step by step:
1. Cookie storage problems
The client proxy class of WebService has a property cookiecontainer that can be used to set or obtain a cookie set. The task of saving the cookie is handed over to him:

System. net. cookiecontainer =   New System. net. cookiecontainer ();

Myservice. WebService Service
=   New App. myservice. WebService ();
Service. cookiecontainer
= Cookiecontainer;


2. we don't want to use web forms for authentication. Fortunately, Asp.. Net Form validation table single page (web. the loginurl in the forms element in the config file can also be specified as a WebService file.
We create a web service dedicated for identity authentication and name it login. asmx, and then make loginurl equal to "login. asmx ", of course, also need to be in the web. in the authorization section of the config file, prohibit Anonymous Access (otherwise we will be busy. the config file is as follows:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System . Web >
< Compilation Debug = "False"   />
< Authentication Mode = "Forms" >
< Forms Name = "Myservice" Loginurl = "Login. asmx" > </ Forms >
</ Authentication >
< Authorization >
< Deny Users = "? " />
</ Authorization >
</ System. Web >
</ Configuration >

In fact, we do not want to turn the browser to login without passing authentication. the real benefit of asmx for WebService client programs is: Anonymous Access to login. methods In asmx (of course, we can also put login. asmx is placed in a separate directory, and anonymous access to the directory is allowed for this purpose, but I think it is more elegant to use loginurl ).
Next, we will add a webmethod for identity authentication for login. asmx:

[Webmethod]
Public   Bool Check ( String Username, String Password)
{
If (Username =   " Aaaaaa "   && Password =   " 123456 " ) // Add verification Logic
{
System. Web. Security. formsauthentication. setauthcookie (username,
False);
Return True;
}

Else
{
Return False;
}

}

The last step is to share the cookiecontainer with the WebService instance in the client program with the login instance.

Class Sample
{
System. net. cookiecontainer
=   New System. net. cookiecontainer ();

Public   Void Login ()
{
Myservicelogin. login Login
=   New App. myservicelogin. login ();
Login. cookiecontainer
= Cookiecontainer;
Login. Check (
" Aaaaaa " , " 123456 " );
}


Public   Void Showhelloworld ()
{
Myservice. WebService Service
= NewApp. myservice. WebService ();
Service. cookiecontainer
=Cookiecontainer;

Console. writeline (service. helloworld ());
}

}

After login () and then showhelloworld (), do you see the familiar "Hello World "? OK, that's easy!

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.