程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> web api 單點登錄(SSO) 權限驗證

web api 單點登錄(SSO) 權限驗證

編輯:C#入門知識

  本文介紹利用web api實現單點登錄,具體原理請看http://www.cnblogs.com/Work-hard/archive/2013/04/10/3011589.html,下面主要介紹相關代碼:

  分站代碼:

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()
{
//令牌驗證結果
if (Request.QueryString["Token"] != null)
{
//持有令牌
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
{
//未持有令牌
Response.Redirect("http://localhost:4213/?BackURL=" + Server.UrlEncode(Request.Url.AbsoluteUri));
}
return View();
}

public void getInfo(string tokenValue)
{ //客戶端調用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"] = a;
System.Web.HttpContext.Current.Session.Timeout = 1;
}
else
{
Response.Redirect("http://localhost:4213/?BackURL=" + Server.UrlEncode(Request.Url.AbsoluteUri));
return;
}
}
}
}

 

主站代碼:

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 = "a";
//public string[] message = { "user", "admin", "tuorist" };
};
HttpResponseMessage response;
LoginController()
{
response = new HttpResponseMessage();
}
[HttpPost]
public HttpResponseMessage Post()
{

//摸擬用戶登錄驗證(帳號、密碼於web.config中)
//真實環境此處應通過數據庫進行驗證
//if (this.txtAccount.Text == System.Configuration.ConfigurationManager.AppSettings["acc"] && this.txtPassport.Text == System.Configuration.ConfigurationManager.AppSettings["pas"])
//{
//產生令牌
string tokenValue = this.getGuidString();
HttpContext.Current.Cache.Insert(tokenValue+"a", tokenValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);

//產生主站憑證
CreateInfo(tokenValue);
response = Request.CreateResponse(HttpStatusCode.OK, @"{""tokenValue"":""" + tokenValue+@"""}");
return response;
}
//產生主站憑證
private void CreateInfo(string tokenValue)
{
infoMassege info = new infoMassege();
info.message = new string[] { "user", "admin", "tuorist" };
info.webName = "a";
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>
/// 產生絕對唯一字符串,用於令牌
/// </summary>
/// <returns></returns>
private string getGuidString()
{
return Guid.NewGuid().ToString().ToUpper();
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved