帮助Janrain参与asp.net表单网站的openID C#代码隐藏
我已经与Janrain Engage签约,加入了其基于openID的免费小部件。但是我不知道应该在C代码中添加什么值。他们提供了一个C#helper类,如下所示:帮助Janrain参与asp.net表单网站的openID C#代码隐藏,c#,asp.net,openid,janrain,C#,Asp.net,Openid,Janrain,我已经与Janrain Engage签约,加入了其基于openID的免费小部件。但是我不知道应该在C代码中添加什么值。他们提供了一个C#helper类,如下所示: using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Web; using System.Xml; usin
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;
public class Rpx
{
private string apiKey;
private string baseUrl;
public Rpx(string apiKey, string baseUrl) {
while (baseUrl.EndsWith("/"))
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}
public string getApiKey() { return apiKey; }
public string getBaseUrl() { return baseUrl; }
public XmlElement AuthInfo(string token) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("token", token);
return ApiCall("auth_info", query);
}
public List<string> Mappings(string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("primaryKey", primaryKey);
XmlElement rsp = ApiCall("mappings", query);
XmlElement oids = (XmlElement)rsp.FirstChild;
List<string> result = new List<string>();
for (int i = 0; i < oids.ChildNodes.Count; i++) {
result.Add(oids.ChildNodes[i].InnerText);
}
return result;
}
public Dictionary<string,ArrayList> AllMappings() {
Dictionary<string,string> query = new Dictionary<string,string>();
XmlElement rsp = ApiCall("all_mappings", query);
Dictionary<string,ArrayList> result = new Dictionary<string,ArrayList>();
XPathNavigator nav = rsp.CreateNavigator();
XPathNodeIterator mappings = (XPathNodeIterator) nav.Evaluate("/rsp/mappings/mapping");
foreach (XPathNavigator m in mappings) {
string remote_key = GetContents("./primaryKey/text()", m);
XPathNodeIterator ident_nodes = (XPathNodeIterator) m.Evaluate("./identifiers/identifier");
ArrayList identifiers = new ArrayList();
foreach (XPathNavigator i in ident_nodes) {
identifiers.Add(i.ToString());
}
result.Add(remote_key, identifiers);
}
return result;
}
private string GetContents(string xpath_expr, XPathNavigator nav) {
XPathNodeIterator rk_nodes = (XPathNodeIterator) nav.Evaluate(xpath_expr);
while (rk_nodes.MoveNext()) {
return rk_nodes.Current.ToString();
}
return null;
}
public void Map(string identifier, string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("identifier", identifier);
query.Add("primaryKey", primaryKey);
ApiCall("map", query);
}
public void Unmap(string identifier, string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("identifier", identifier);
query.Add("primaryKey", primaryKey);
ApiCall("unmap", query);
}
private XmlElement ApiCall(string methodName, Dictionary<string,string> partialQuery) {
Dictionary<string,string> query = new Dictionary<string,string>(partialQuery);
query.Add("format", "xml");
query.Add("apiKey", apiKey);
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> e in query) {
if (sb.Length > 0) {
sb.Append('&');
}
sb.Append(System.Web.HttpUtility.UrlEncode(e.Key, Encoding.UTF8));
sb.Append('=');
sb.Append(HttpUtility.UrlEncode(e.Value, Encoding.UTF8));
}
string data = sb.ToString();
Uri url = new Uri(baseUrl + "/api/v2/" + methodName);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
// Write the request
StreamWriter stOut = new StreamWriter(request.GetRequestStream(),
Encoding.ASCII);
stOut.Write(data);
stOut.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream ();
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(dataStream);
XmlElement resp = doc.DocumentElement;
if (resp == null || !resp.GetAttribute("stat").Equals("ok")) {
throw new Exception("Unexpected API error");
}
return resp;
}
public static void Main(string[] args) {
Rpx r = new Rpx(args[0], args[1]);
if (args[2].Equals("mappings")) {
Console.WriteLine("Mappings for " + args[3] + ":");
foreach(string s in r.Mappings(args[3])) {
Console.WriteLine(s);
}
}
if (args[2].Equals("all_mappings")) {
Console.WriteLine("All mappings:");
foreach (KeyValuePair<string, ArrayList> pair in r.AllMappings()) {
Console.WriteLine(pair.Key + ":");
foreach (string identifier in pair.Value) {
Console.WriteLine(" " + identifier);
}
}
}
if (args[2].Equals("map")) {
Console.WriteLine(args[3] + " mapped to " + args[4]);
r.Map(args[3], args[4]);
}
if (args[2].Equals("unmap")) {
Console.WriteLine(args[3] + " unmapped from " + args[4]);
r.Unmap(args[3], args[4]);
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
Net系统;
使用系统文本;
使用System.Web;
使用System.Xml;
使用System.Xml.XPath;
公共类Rpx
{
私钥;
私有字符串baseUrl;
公共Rpx(字符串apiKey、字符串baseUrl){
while(baseUrl.EndsWith(“/”)
baseUrl=baseUrl.Substring(0,baseUrl.Length-1);
this.apiKey=apiKey;
this.baseUrl=baseUrl;
}
公共字符串getApiKey(){return apiKey;}
公共字符串getBaseUrl(){return baseUrl;}
公共XmlElement AuthInfo(字符串标记){
字典查询=新建字典();
查询.添加(“令牌”,令牌);
返回ApiCall(“验证信息”,查询);
}
公共列表映射(字符串primaryKey){
字典查询=新建字典();
query.Add(“primaryKey”,primaryKey);
xmlementrsp=ApiCall(“映射”,查询);
xmlement OID=(xmlement)rsp.FirstChild;
列表结果=新列表();
对于(int i=0;i0){
某人附加('&');
}
sb.Append(System.Web.HttpUtility.UrlEncode(e.Key,Encoding.UTF8));
某人附加('=');
sb.Append(HttpUtility.UrlEncode(e.Value,Encoding.UTF8));
}
字符串数据=sb.ToString();
Uri url=新Uri(baseUrl+“/api/v2/”+methodName);
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.Method=“POST”;
request.ContentType=“application/x-www-form-urlencoded”;
request.ContentLength=data.Length;
//写下请求
StreamWriter stOut=新的StreamWriter(request.GetRequestStream(),
编码(ASCII);
写(数据);
stOut.Close();
HttpWebResponse=(HttpWebResponse)request.GetResponse();
Stream dataStream=response.GetResponseStream();
XmlDocument doc=新的XmlDocument();
doc.PreserveWhitespace=false;
文档加载(数据流);
XmlElement resp=doc.DocumentElement;
如果(resp==null | |!resp.GetAttribute(“stat”).Equals(“ok”)){
抛出新异常(“意外的API错误”);
}
返回响应;
}
公共静态void Main(字符串[]args){
Rpx r=新的Rpx(args[0],args[1]);
if(args[2].Equals(“映射”)){
WriteLine(“+args[3]+”:”的映射);
foreach(r.Mappings中的字符串s(args[3])){
控制台。写入线(s);
}
}
if(args[2].Equals(“所有_映射”)){
Console.WriteLine(“所有映射:”);
foreach(r.AllMappings()中的KeyValuePair对){
Console.WriteLine(pair.Key+“:”);
foreach(成对的字符串标识符.Value){
Console.WriteLine(“+”标识符);
}
}
}
if(args[2].Equals(“map”)){
Console.WriteLine(args[3]+”映射到“+args[4]);
r、 地图(args[3],args[4]);
}
if(args[2].Equals(“取消映射”)){
Console.WriteLine(args[3]+“未映射自”+args[4]);
r、 取消映射(参数[3],参数[4]);
}
}
}
请原谅,我不是C#的高手,但我不知道把apiKey和token#url的值放在哪里。此外,如果用户登录如何显示用户名,该用户名来源于他用于注册的帐户,则为Google或Yahoo!例如
此外,没有提供注销选项。
任何帮助都将不胜感激,因为Janrain开发者的帮助毫无用处。I agre
var token = Request.Form["token"];
var rpx = new Helpers.Rpx("your-api-key-goes-here", "https://rpxnow.com");
var authInfo = rpx.AuthInfo(token);
var doc = XDocument.Load(new XmlNodeReader(authInfo));
ViewData["displayName"] = doc.Root.Descendants("displayName").First().Value;
ViewData["identifier"] = doc.Root.Descendants("identifier").First().Value;