我应该从服务层返回什么? DotNetOpenAuth

发布于 2024-10-12 14:34:20 字数 1597 浏览 1 评论 0原文

我想知道在这种情况下我应该返回什么。我的用户界面和服务层位于不同的项目中。

这就是发生的事情

->用户来到网站 ->用户选择 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;
        }

Now 中,这是我迷失的地方,因为它们是我可以返回的很多东西。

我可以返回

return req.RedirectingResponse.AsActionResult()

但是我认为这会很糟糕,因为现在我依赖于 asp.net mvc ActionResult 并且如果我说将此服务层用于其他一些项目(也许我有一个连接到移动应用程序的 Web 服务)。它不会运作得很好。

我可以返回 OutgoingWebResponse,但我不太确定在取回它后该如何处理它。

我还可以返回从 CreateRequest() 生成的 IAuthenticationRequest

最后,我可以返回我的域对象(OpenId)以及上面列出的对象之一。

I am wondering what should I return in this case. My Ui and service layer are in different projects.

This is what happens

-> User Comes to Site -> User chooses openId provider and hits login -> Post back to controller

 [HttpPost]
        public ActionResult Login(LoginViewModel loginViewModel)
        {
           var test = accountService.SendOpenIdRequest(loginViewModel.OpenId);

        }


public class LoginViewModel
    {
        public OpenId OpenId { get; set; }
    }

So I take in a ViewModel that contains my domain class.

 public class OpenId
    {
        public string Url { get; set; }
    }

So far in my 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;
        }

Now this is where I get lost since their are so many things I could return.

I could return

return req.RedirectingResponse.AsActionResult()

However I think this would be bad as now I am depending on asp.net mvc ActionResult and if I say use this service layer for some other project(maybe I have a webservice that connects to a mobile application). It won't won't work to well.

I could return OutgoingWebResponse but I am not really sure what to do with it once I get it back.

I could also return the IAuthenticationRequest what is generated from CreateRequest()

Finally I could return my Domain Object(OpenId) with one of the ones I listed above in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-10-19 14:34:20

您可以返回一个 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();
}

You could return an 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;
    }
}

and then in your controller:

[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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文