調用wcf增加token身份驗證


今天遇到一個坑爹的項目需求,做個筆記方便下次再用。

需求描述:

1.打開對方官網https:test.cn

2.用賬戶登陸https:test.cn

3.登陸成功后跳轉到我們的網站https:my.cn,並且返回一個code給我們

4.進入我們系統后根據返回的code在后台用代碼post對方的一個action 地址:https://https:test.cn/CNS-AS/OAuth/Token/1.1 獲取一個Access_token

5.將獲取到的Access_token添加到http 頭文件中再去調用對方的wcf接口https:test.cn/CNS-Service/CNSServices.svc/CNSServices

6.最后拿到驗證通過的賬戶信息,最后停留https:my.cn 我們站點進行相關操作

廢話少說,直接上代碼

1.https:test.cn 登陸成功后跳轉的地址:

https://test.cn/CNS-AS/OAuth/Authorize?client_id=indegene_client&redirect_uri=https:my.cn/home/index&state=12321&scope=http://tempuri.org/ICNSServices/GetCNSUserName&response_type=code

2.進入https:my.cn后代碼

public ActionResult Index()
{

//1-3step(Authorization Response)

string code = Request["code"];
string state = Request["state"];

//4. Access Token Request

HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("https://test.cn/CNS-AS/OAuth/Token/1.1"); //請求地址

//設置用戶名密碼的Base64編碼
string code1 = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "test", "test")));

string postData = string.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}", code, "http://my/home/index", "indegene_client", "indegene_secretIJH"); // 要發放的數據
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 
objWebRequest.Method = "POST";//提交方式

objWebRequest.ContentType = "application/x-www-form-urlencoded";
objWebRequest.ContentLength = byteArray.Length;
Stream newStream = objWebRequest.GetRequestStream(); // Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //寫入參數
newStream.Close();

//響應請求

//5. Access Token Response
HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();//獲取響應
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string textResponse = sr.ReadToEnd(); // 返回的數據
Response.Write(textResponse);//打印返回值
AccessToken access_token = JsonConvert.DeserializeObject<AccessToken>(textResponse);

//6. Request CNS user information by access token(調用wcf)
CNS.CNSServicesClient svc = new CNS.CNSServicesClient();
using (OperationContextScope scope =
new OperationContextScope(svc.InnerChannel))
{
//添加消息頭
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers.Add("Authorization", "aaa" + access_token.Access_token);
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

//調用wcf(必須先添加消息頭,否則不能調用(身份驗證))
string res = svc.GetCNSUserName();

}

}

 

public class AccessToken {

private string access_token;

public string Access_token
{
get { return access_token; }
set { access_token = value; }
}
private string token_type;

public string Token_type
{
get { return token_type; }
set { token_type = value; }
}
private string expires_in;

public string Expires_in
{
get { return expires_in; }
set { expires_in = value; }
}
private string refresh_token;

public string Refresh_token
{
get { return refresh_token; }
set { refresh_token = value; }
}
private string scope;

public string Scope
{
get { return scope; }
set { scope = value; }
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM