我正在尝试让 SSO openid 与 dotnetopenauth 一起使用。
我有两个独立的项目,分别调试(都在本地主机上,但有两个不同的端口),一个作为提供者,一个作为依赖方。
依赖方正在运行localhost:1903
。提供程序正在运行localhost:3314
。
依赖方代码:
public ActionResult Authenticate()
{
UriBuilder returnToBuilder = new UriBuilder(Request.Url);
returnToBuilder.Path = "/OpenId/";
returnToBuilder.Query = null;
returnToBuilder.Fragment = null;
Uri returnTo = returnToBuilder.Uri;
returnToBuilder.Path = "/";
Realm realm = returnToBuilder.Uri;
realm = "http://localhost:3314/OpenId/";
returnTo = new Uri("http://localhost:3314/OpenId/");
var response = openid.GetResponse();
if (response == null) {
if (Request.QueryString["ReturnUrl"] != null && User.Identity.IsAuthenticated) {
} else {
string strIdentifier = "testidentifier";
var request = openid.CreateRequest(
strIdentifier,
realm,
returnTo);
var fetchRequest = new FetchRequest();
request.AddExtension(fetchRequest);
request.RedirectToProvider();
}
} else {
switch (response.Status) {
case AuthenticationStatus.Canceled:
//stuff got cancelled for some reason
break;
case AuthenticationStatus.Failed:
//response.Exception.Message;
break;
case AuthenticationStatus.Authenticated:
//a bunch of applying roles that i don't think we care about
break;
}
}
return new EmptyResult();
}
提供者代码:
public ActionResult Index()
{
IAuthenticationRequest iR = (IAuthenticationRequest)Request;
if (iR.IsReturnUrlDiscoverable(ProviderEndpoint.Provider.Channel.WebRequestHandler) != RelyingPartyDiscoveryResult.Success) {
iR.IsAuthenticated = false;
return new EmptyResult();
}
if (iR.IsDirectedIdentity) {
if (User.Identity.IsAuthenticated) {
iR.LocalIdentifier = BuildIdentityUrl();
iR.IsAuthenticated = true;
} else {
if (iR.Immediate || ImplicitAuth) {
iR.IsAuthenticated = false;
} else {
if (!Request.Path.EndsWith("Login", StringComparison.OrdinalIgnoreCase)) {
return RedirectToAction("Login", "User");
}
}
}
} else {
string userOwningOpenIdUrl = ExtractUserName(iR.LocalIdentifier);
iR.IsAuthenticated = userOwningOpenIdUrl == User.Identity.Name;
if (!iR.IsAuthenticated.Value && !ImplicitAuth && !iR.Immediate) {
if (!Request.Path.EndsWith("Login", StringComparison.OrdinalIgnoreCase)) {
return RedirectToAction("Login", "User");
}
}
}
if (iR.IsAuthenticated.Value) {
var fetchRequest = iR.GetExtension<FetchRequest>();
if (fetchRequest != null) {
var fetchResponse = new FetchResponse();
//roles and stuff
iR.AddResponseExtension(fetchResponse);
}
}
return new EmptyResult();
}
在方法上运行依赖方代码时出现错误openid.CreateRequest
。我在我的提供程序代码上启用了调试,它永远不会受到影响。
研究错误,我发现了很多关于代理问题的建议,但这对我来说不应该是一个问题,因为我只去本地主机。
也许这是相当明显的事情,但我不知道我做错了什么。
在此先感谢您的帮助!
编辑:仅供参考,我从 DotNetOpenAuth 示例中获得了此代码。