我自己在我的应用程序中使用身份验证交换在c#中通过Dotnetopenauth实现SSO
我的怀疑如下,
请求代码:
IAuthenticationRequest request = openid.CreateRequest(txt_openid_identifier.Text);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.Clear();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last); fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
request.AddExtension(fetchRequest);
// Issue request to OP
request.RedirectToProvider();
回应代码:
using (var openid = new OpenIdRelyingParty())
{
var response = openid.GetResponse();
if (response == null) return;
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
string emailID = string.Empty;
Session["email"] = string.Empty;
Session["name"] = string.Empty;
Session["country"] = string.Empty;
Session["Accesskey"] = string.Empty;
Session["SecretAccesskey"] = string.Empty;
var fetchResponse = response.GetExtension<FetchResponse>();
if (fetchResponse.Attributes.Contains(WellKnownAttributes.Contact.Email))
{
IList<string> emailAddresses =
fetchResponse.Attributes[WellKnownAttributes.Contact.Email].Values;
emailID = emailAddresses.Count > 0 ? emailAddresses[0] : null;
}
else
emailID = string.Empty;
}
}
答案 0 :(得分:0)
我发现某些提供商(即myopenid.com)为FetchRequest
返回null,同时返回ClaimsRequest
的有效数据。看来不同的提供商支持不同的属性交换机制。在请求中:
var request = openid.CreateRequest(Request.Form["openid_identifier"]);
var sr = new ClaimsRequest
{
Email = DemandLevel.Request,
FullName = DemandLevel.Request
};
request.AddExtension(sr);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddOptional(WellKnownAttributes.Name.FullName);
request.AddExtension(fetchRequest);
return request.RedirectingResponse.AsActionResult();
并在收到回复时:
string email, fullName = String.Empty;
var fetch = response.GetExtension<FetchResponse>();
if (fetch != null)
{
email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
fullName = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
}
var claimResponse = response.GetExtension<ClaimsResponse>();
if (claimResponse != null)
{
email = claimResponse.Email;
fullName = claimResponse.FullName;
}
因此,我们尝试两种方式,并希望从至少一种方法中获取数据。