Search code examples
asp.net-mvc-2service-layer

What should I return from my service layer? DotNetOpenAuth


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.


Solution

  • 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();
    }