C # authentication when calling Web Service

Source: Internet
Author: User
Tags define local

In project development, we often use WebService, but when using WebService, we often consider the following question: how to prevent others from accessing my WebService? Where can I reference my WebService? For the first question, WebService is a security issue, because the WebService we provide does not allow anyone to reference it. It may only allow the company or authorized personnel to use it. So how can we prevent unauthorized user access? It is easy to think of using a group of user names and passwords to prevent unauthorized calls.
In System. Net, a NetworkCredential is provided. With NetworkCredential, we can provide a credential in the network. Only the user who obtains the credential can access the corresponding service permissions. NetworkCredential is also used here. In NetworkCredential, we provide the name of the server where the WebService is released, and the username and password for logging on to the server and calling the WebService (configured in IIS ).
When WebService is called, set its Credential attribute and assign the Credential obtained above to it. In this way, only the provided user name and password can be used to call WebService, while other users cannot access it, this can prevent WebService from being called by others.
For host names, usernames, and passwords, you can use webconfig for B/S and the application configuration file for C/S. In this way, the configuration is flexible.
Take C/S as an example. First, we provide a server network credential and then use WebRequest to verify whether the connection is successful. Of course, you can encrypt the user name and password to ensure security.

The main source code is as follows:
Code
1/** // <summary>
2 // server network credential
3 /// </summary>
4 /// <returns> </returns>
5 public static NetworkCredential MyCred ()
6 {
7 string loginUser = Properties. Settings. Default. UserName; // User Name
8 string loginPSW = Properties. Settings. Default. UserPSW; // Password
9 string loginHost = Properties. Settings. Default. HostName; // host name, which can be an IP address or a server name
10 NetworkCredential myCred = new NetworkCredential (loginUser, loginPSW, loginHost );
11 // NetworkCredential myCred = new NetworkCredential ("username", "123456", "yourip"); // "username", "123456", "yourservername"
12 return myCred;
13}
14/** // <summary>
15 // verify whether the connection is successful to the server. If the connection is successful, TRUE is returned.
16 /// </summary>
17 /// <param name = "url"> server WebService URL </param>
18 /// <returns> </returns>
19 public static bool Credential (string url)
20 {
21 // define local variables
22 string url = G_Url; // 2009-02-25 Chen huicong server verification only verifies to the machine
23
24 try
25 {
26 if (myWebResponse = null)
27 {
28 WebRequest myWebRequest = WebRequest. Create (url); // Create a connection request based on the URL
29 myWebRequest. Credentials = MyCred (); // obtain the verification credential. This is the most important sentence.
30 myWebRequest. Timeout = 20000; // unit: milliseconds
31
32 myWebResponse = myWebRequest. GetResponse (); // return the message when the connection is successful.
33}
34}
35 catch (WebException wex) // unable to connect to the server, probably because of a server error or incorrect user name and password
36 {
37 if (myWebResponse! = Null) // destroy sale
38 {
39 myWebResponse. Close ();
40 myWebResponse = null;
41}
42
43 return false;
44}
45 catch (Exception ex)
46 {
47 if (myWebResponse! = Null)
48 {
49 myWebResponse. Close ();
50 myWebResponse = null;
51}
52
53 return false;
54
55}
56 finally
57 {
58}
59
60 return true;
61}
62
63 private static WS_Webasic.WS_Webasic webasic = null; // implement WS_Webasic.WS_Webasic
64
65/*** // <summary>
66 // WS_Webasic Initialization
67 /// </summary>
68 public static WS_Webasic.WS_Webasic WS_Webasic
69 {
70

Related Article

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.