我应该从我的服务层返回什么? dotnetopenauth
-
11-10-2019 - |
题
我想知道在这种情况下我应该返回什么。我的UI和服务层在不同的项目中。
这就是发生的事情
- >用户进入网站 - >用户选择OpenID提供商并登录登录 - >发布回控制器
[HttpPost]
public ActionResult Login(LoginViewModel loginViewModel)
{
var test = accountService.SendOpenIdRequest(loginViewModel.OpenId);
}
public class LoginViewModel
{
public OpenId OpenId { get; set; }
}
因此,我接受包含我的域类的ViewModel。
public class OpenId
{
public string Url { get; set; }
}
到目前为止我的sendopenidrequest
public ? SendOpenIdRequest(OpenId openId)
{
var openIdRelyingParty = new OpenIdRelyingParty();
var response = openIdRelyingParty.GetResponse();
Identifier id;
if (Identifier.TryParse(openId.Url, out id))
{
try
{
var req = openIdRelyingParty.CreateRequest(openId.Url);
return req.RedirectingResponse
}
catch (ProtocolException ex)
{
}
}
return null;
}
现在,这就是我迷路的地方,因为他们可以返回很多东西。
我可以回来
return req.RedirectingResponse.AsActionResult()
但是,我认为这会很糟糕,因为现在我依靠ASP.NET MVC ActionResult,如果我说将此服务层用于其他项目(也许我有一个连接到移动应用程序的网络服务)。它不会很好。
我可以 return OutgoingWebResponse
但是我不确定一旦我收回它该怎么做。
我还可以返回createrequest()产生的iauthenticationrequest
最后,我可以将我的域对象(OpenID)返回上面列出的一个。
解决方案
你可以返回 OutgoingWebResponse
:
public OutgoingWebResponse SendOpenIdRequest(OpenId openId)
{
using (var openIdRelyingParty = new OpenIdRelyingParty())
{
var response = openIdRelyingParty.GetResponse();
Identifier id;
if (Identifier.TryParse(openId.Url, out id))
{
try
{
var req = openIdRelyingParty.CreateRequest(openId.Url);
return req.RedirectingResponse
}
catch (ProtocolException ex)
{
}
}
return null;
}
}
然后在您的控制器中:
[HttpPost]
public ActionResult Login(LoginViewModel loginViewModel)
{
var response = accountService.SendOpenIdRequest(loginViewModel.OpenId);
if (response == null)
{
ModelState.AddModelError(
"openid_identifier",
"The specified login identifier is invalid"
);
return View();
}
return response.AsActionResult();
}
不隶属于 StackOverflow