The configuration is related to the OAuth Protocol. To understand the OAuth protocol, you must understand the http get/POST method. Therefore, we have studied how to use Web APIs or MVC to construct POST messages and implement interaction between the client and the server.
The tool I used is Visual Studio 2013 + Web API 2 + MVC 5.
Create two Web projects in two different VS2013 instances, and select an empty template. One of them is named "Client", the other is named "Server", and the other is named "Web API.
Two different VS2013 instances are required to enable the two IIS Express services to run simultaneously on the same machine.
Let's take a look at the client:
Because it is only a Demo, we only create an empty MVC controller in the client and name it HomeController. It comes with an Index () method. Here we write the code for constructing a request and sending the request:
Copy code
Namespace Client. Controllers
{
Public class HomeController: Controller
{
//
// GET:/Home/
Public ActionResult Index ()
{
String url = ""; // here we do not know the server url, so leave it blank
# Region construct a POST request
HttpWebRequest request = WebRequest. Create (url) as HttpWebRequest;
Request. Method = "POST ";
Request. ContentType = "application/x-www-form-urlencoded ";
Request. Host = "localhost: 14340 ";
String body = "client_id = 123 & redirect_uri =" + url + "& response_type = code ";
Byte [] bodyBytes = Encoding. UTF8.GetBytes (body );
Stream requestStream = request. GetRequestStream ();
RequestStream. Write (bodyBytes, 0, bodyBytes. Length );
# Endregion
# Region sends a request and obtains a response
// The HttpWebRequest. GetResponse () method must be used to send a request and obtain a response.
HttpWebResponse response = request. GetResponse () as HttpWebResponse;
StreamReader sr = new StreamReader (response. GetResponseStream ());
ViewBag. Response = sr. ReadToEnd ();
# Endregion
Return View ();
}
}
}
Copy code
Next we construct the server:
Create a Web API 2 Controller in the Server project. Naming is arbitrary (I name it ApiTestController here). Write a Post method to the Controller and return the HttpResponseMessage type.
I always thought that the POST parameter could be obtained in the Request attribute, but I checked all the attributes and methods of the Request attribute carefully, including the extension method, none of them found the attributes or methods for Directly Reading the POST message request body. According to the materials, if you want to obtain the content of the request body in the POST request, you must encapsulate the parameters in a class. Therefore, we create a class RequestArgs as follows:
Copy code
Namespace Server. Models
{
///
/// For post requests, a model class must be created for all parameters of the Request body.
///
Public class RequestArgs
{
Public string client_id {get; set ;}
Public string redirect_uri {get; set ;}
Public string response_type {get; set ;}
}
}
Copy code
The parameters here must be the same as the request message content in the preceding client Action method (including case sensitive)
Then we can write the POST method to the Controller as follows:
Copy code
Namespace Server. Controllers
{
Public class ApiTestController: ApiController
{
Public HttpResponseMessage Post (RequestArgs args)
{
String client_id = args. client_id;
String redirect_uri = args. redirect_uri;
String response_type = args. response_type;
Return Request. CreateResponse (HttpStatusCode. OK, "I have received ed your message. Client_id =" + client_id + ", RedirectUri =" + redirect_uri + "response_type =" + response_type );
}
}
}
Copy code
Here we directly return the message content. In actual projects, we can process the input parameters ourselves.
Then compile and run the server (preferably press F5 to enter the debugging mode ). The Web page appears to be an error page, but you don't need to worry about it. Our server has started running.
Write down the server address (generally the port number will change) and enter the server address in the client url variable.
Compile and run the client. If the returned information ("I have received ed your message" and parameter information) is displayed, the request is successful.