我正在尝试在ASPX Web应用程序中运行OpenId登录,我不知道出了什么问题。
这里首先是一个有效的版本,但是我无法使用它,因为Yadis由于重定向而无法验证依赖方,这会导致出现“你确定要将数据发送给可疑客户端”的警告?”由openID服务器。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
OpenIdRelyingParty openId = new OpenIdRelyingParty();
IAuthenticationResponse response = openId.GetResponse();
ValidateLogin(response);
if (response == null || response.Status != AuthenticationStatus.Authenticated)
{
logger.Info("Not logged in.");
Login(openId); //<-- this is why yadis discovery fails. When parsing the the page finding <meta http-equiv="X-XRDS-Location" content="https://testServer.domain.cz/openidtrial/Content/yadis.xml" />
}
}
}
void DoTry(string identifier, Action action)
{
try
{
action();
}
catch (Exception exception)
{
logger.ErrorException("Error getting response " + identifier, exception);
}
}
void Login(OpenIdRelyingParty openId)
{
var request = openId.CreateRequest(Identifier.Parse("https://openId.server.address"));
request.AddExtension(new ClaimsRequest()
{
Nickname = DemandLevel.Request
});
logger.Info("sending request");
request.RedirectToProvider();
}
void ValidateLogin(IAuthenticationResponse response)
{
logger.Info(string.Format("response is null: {0}", response == null));
if (response != null)
{
logger.Info(string.Format("response status is: {0}", response.Status));
}
if (response != null && response.Status == AuthenticationStatus.Authenticated)
{
logger.Info("authorization request received");
logger.Info("ClaimedIdentifier " + response.ClaimedIdentifier);
//var claimUntrusted = response.GetUntrustedExtension<ClaimsResponse>();
var claim = response.GetExtension<ClaimsResponse>();
//logger.Info(string.Format("claimUntrusted is {0}", claimUntrusted != null ? "value" : "null"));
logger.Info(string.Format("claim is {0}", claim != null ? "value" : "null"));
DoTry("claim", () => logger.Info("claim nick " + claim.Nickname));
//DoTry("claim untrusted", () => logger.Info("untrusted claim nick " + claimUntrusted.Nickname));
}
}
以下是日志文件的内容:
Info|12:19:06,198|38-|Message:response is null: True| <--start up
Info|12:19:06,214|38-|Message:Not logged in.|
Info|12:19:07,602|38-|Message:sending request| <--auto redirection
Info|12:19:14,248|18-|Message:response is null: True| <-- Yadis discovery
Info|12:19:14,248|18-|Message:Not logged in.|
Info|12:19:14,497|18-|Message:sending request|
Info|12:19:16,853|38-|Message:response is null: True|
Info|12:19:16,853|38-|Message:Not logged in.|
Info|12:19:17,134|38-|Message:sending request|
Info|12:19:17,633|21-|Message:response is null: False|
Info|12:19:17,633|21-|Message:response status is: Failed|
Info|12:19:17,633|21-|Message:Not logged in.|
Info|12:19:17,898|21-|Message:sending request|
Info|12:19:18,085|38-|Message:response is null: True|
Info|12:19:18,085|38-|Message:Not logged in.|
Info|12:19:18,335|38-|Message:sending request|
Info|12:19:19,910|18-|Message:response is null: True|
Info|12:19:19,910|18-|Message:Not logged in.|
Info|12:19:20,176|18-|Message:sending request|
Info|12:19:20,909|38-|Message:response is null: False| <-- successful login
Info|12:19:20,909|38-|Message:response status is: Authenticated|
Info|12:19:20,909|38-|Message:authorization request received|
Info|12:19:20,909|38-|Message:ClaimedIdentifier ***identifier***|
Info|12:19:20,909|38-|Message:claim is value|
Info|12:19:20,909|38-|Message:claim nick testnick|
我试图通过删除自动重写来解决重定向问题。我添加了一个按钮,可以手动进行重定向。代码的改变部分如下:
protected void Page_Load(object sender, EventArgs e)
{
//logger.Info(string.Format("request Path: {0}", Request.Url.AbsolutePath));
//logger.Info(string.Format("Host name: {0}, Host IP: {1}", Request.UserHostName, Request.UserHostAddress));
if (!IsPostBack)
{
OpenIdRelyingParty openId = new OpenIdRelyingParty();
IAuthenticationResponse response = openId.GetResponse();
ValidateLogin(response);
if (response == null || response.Status != AuthenticationStatus.Authenticated)
{
logger.Info("Not logged in.");
//Login(openId);
}
}
}
protected void Login_Click(object sender, EventArgs e)
{
OpenIdRelyingParty openId = new OpenIdRelyingParty();
Login(openId);
}
但是在使用它时,在验证后,response.Status 总是 AuthenticationStatus.Failed
以下是日志文件的内容:
Info|12:14:41,481|36-|Message:response is null: True| <--start up
Info|12:14:41,497|36-|Message:Not logged in.|
Info|12:14:55,490|18-|Message:sending request| <--login button clicked
Info|12:15:01,543|18-|Message:response is null: True| <-- yadis discovery
Info|12:15:01,543|18-|Message:Not logged in.|
Info|12:15:03,540|28-|Message:response is null: True|
Info|12:15:03,540|28-|Message:Not logged in.|
Info|12:15:03,633|21-|Message:response is null: False| <-- response received
Info|12:15:03,633|21-|Message:response status is: Failed|
Info|12:15:03,633|21-|Message:Not logged in.|
我还尝试使用static OpenIdRelyingParty openId
而不是在方法中创建新实例。 - 没有帮助
此外,我试图将openId存储在这样的会话中 - 没有帮助:
OpenIdRelyingParty OpenId
{
get
{
if (HttpContext.Current.Session["openIdSession"] == null)
{
logger.Info("Creating OpenId");
HttpContext.Current.Session["openIdSession"] = new OpenIdRelyingParty();
}
return (OpenIdRelyingParty)HttpContext.Current.Session["openIdSession"];
}
}
有人知道出了什么问题吗?
答案 0 :(得分:0)
我已经解决了我的问题。 我从上面的帖子中获取了一个原始代码(第一个,它是可操作的,但不是&#34;用户友好&#34;因为yadis发现失败,我添加了一个明确的XRDS请求处理。 最好添加一个ashx处理程序来处理yadis,但这也有效。
以下是代码:
protected void Page_Load(object sender, EventArgs e)
{
//XRDS request handling that solves the yadis discovery problem
if (Request.AcceptTypes.Any(a => a.Contains("xrds")))
{
Response.Clear();
Response.ContentType = "application/xrds+xml";
string path = Server.MapPath(Request.ApplicationPath + "/Content/yadis.xml");
logger.Info(string.Format("yadis path: {0}", path));
Response.WriteFile(path);
Response.End();
return;
}
if (!IsPostBack)
{
OpenIdRelyingParty openId = new OpenIdRelyingParty();
IAuthenticationResponse response = openId.GetResponse();
ValidateLogin(response);
if (response == null || response.Status != AuthenticationStatus.Authenticated)
{
logger.Info("Not logged in.");
Login(openId); //<-- this is why yadis discovery failed.
}
}
}