This article describes how to use web APIs to implement Single-point logon. For details, refer:
Substation code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text. RegularExpressions;
Using System. Web;
Using System. Web. Mvc;
Using System. Net. Http;
Namespace MvcApplication2.Controllers
{
Public class HomeController: Controller
{
Public class infoMassege
{
Public string webName {get; set ;}
Public string [] message {get; set ;}
}
Public ActionResult Index ()
{
// Token Verification Result
If (Request. QueryString ["Token"]! = Null)
{
// Hold the token
String tokenValue = Request. QueryString ["Token"];
HttpCookie tokenCookie = new HttpCookie ("Token ");
TokenCookie. Values. Add ("Value", tokenValue );
TokenCookie. Expires = DateTime. Now. Add (new TimeSpan (24, 0, 0 ));
TokenCookie. Path = "/";
Response. AppendCookie (tokenCookie );
If (Session ["Info"] = null)
{
GetInfo (tokenValue );
}
}
Else if (Request. Cookies ["Token"]! = Null)
{
String tokenValue = Convert. ToString (Request. Cookies ["Token"]. Value );
If (Session ["Info"] = null)
{
GetInfo (tokenValue );
}
}
Else
{
// Token not held
Response. Redirect ("http: // localhost: 4213 /? BackURL = "+ Server. UrlEncode (Request. Url. AbsoluteUri ));
}
Return View ();
}
Public void getInfo (string tokenValue)
{// The client calls the Web api
System. Net. Http. HttpClient client = new System. Net. Http. HttpClient ();
Client. BaseAddress = new Uri ("http: // localhost: 4213 /");
Client. DefaultRequestHeaders. Accept. Add (new System. Net. Http. Headers. MediaTypeWithQualityHeaderValue ("application/json "));
HttpResponseMessage response = client. GetAsync ("api/Login /? Token = "+ tokenValue). Result;
If (response. IsSuccessStatusCode)
{
InfoMassege info = response. Content. ReadAsAsync <infoMassege> (). Result;
String [] a = info. message;
System. Web. HttpContext. Current. Session ["Info"] =;
System. Web. HttpContext. Current. Session. Timeout = 1;
}
Else
{
Response. Redirect ("http: // localhost: 4213 /? BackURL = "+ Server. UrlEncode (Request. Url. AbsoluteUri ));
Return;
}
}
}
}
Main Site code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Net. Http;
Using System. Web;
Using System. Web. Http;
Using SSO. Passport. Class;
Using System. Data;
Namespace MvcApplication1.Controllers
{
Public class LoginController: ApiController
{
Public class infoMassege
{
Public string webName {get; set ;}
Public string [] message {get; set ;}
// Public string webName = "";
// Public string [] message = {"user", "admin", "tuorist "};
};
HttpResponseMessage response;
LoginController ()
{
Response = new HttpResponseMessage ();
}
[HttpPost]
Public HttpResponseMessage Post ()
{
// Verify the user login (the account and password are in web. config)
// Verify the real environment through the database
// If (this.txt Account. text = System. configuration. configurationManager. appSettings ["acc"] & this.txt Passport. text = System. configuration. configurationManager. appSettings ["pas"])
//{
// Generate a token
String tokenValue = this. getGuidString ();
HttpContext. Current. Cache. Insert (tokenValue + "a", tokenValue, null, DateTime. Now. AddMinutes (3), TimeSpan. Zero );
// Generate the master site credential
CreateInfo (tokenValue );
Response = Request. CreateResponse (HttpStatusCode. OK, @ "{" "tokenValue" ":" + tokenValue + @"""}");
Return response;
}
// Generate the master site credential
Private void CreateInfo (string tokenValue)
{
InfoMassege info = new infoMassege ();
Info. message = new string [] {"user", "admin", "tuorist "};
Info. webName = "";
If (HttpContext. Current. Cache [tokenValue] = null)
{
HttpContext. Current. Cache. Insert (tokenValue, info, null, DateTime. Now. AddMinutes (1), TimeSpan. Zero );
}
// System. Web. Caching. Cache c = HttpContext. Current. Cache;
// CacheManager. TokenInsert (tokenValue, info, DateTime. Now. AddMinutes (100 ));
}
[HttpGet]
Public HttpResponseMessage getCache (string token)
{
If (HttpContext. Current. Cache [token + "a"]! = Null)
{
InfoMassege proof = new infoMassege ();
Proof = (infoMassege) HttpContext. Current. Cache [token];
If (proof! = Null)
{
Response = Request. CreateResponse (HttpStatusCode. OK, proof );
}
Else
{
CreateInfo (token );
GetCache (token );
}
}
Else
{
Response = Request. CreateResponse (HttpStatusCode. Forbidden );
}
Return response;
}
/// <Summary>
/// Generate an absolutely unique string for the token
/// </Summary>
/// <Returns> </returns>
Private string getGuidString ()
{
Return Guid. NewGuid (). ToString (). ToUpper ();
}
}
}