Umbraco Async SurfaceController

602 Views Asked by At

I am working on a project (ASP.NET MVC 5) where I am using Umbraco 7.4.3. I am trying to implement the google analytics api along with oauth2. I used the sample code available on the google documentation platform. After authorizing with my google account I get a correct refresh token. But the problem is this refresh token is returned in the URL and is not getting passed by my controller to my view which remains empty. I have a feeling that my controller does not wait to execute it's code after the user authorized his or her google account hence the controller is not bothered about the await operator.

Link to the sample code

public class GoogleAnalyticsController : SurfaceController
{
    public async Task<ActionResult> Add(CancellationToken cancellationToken) 
    {
        var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);

        if (result.Credential != null)
        {
            var service = new AnalyticsService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = "Analytics Dashboard"
                });

            // YOUR CODE SHOULD BE HERE..
            ViewBag.AccessToken = result.Credential.Token.AccessToken;
            ViewBag.RefreshToken = result.Credential.Token.RefreshToken;

            var list = await service.Management.AccountSummaries.List().ExecuteAsync(cancellationToken);
            ViewBag.Username = list.Username;

            for (int i = 0; i < list.TotalResults; i++)
            {
                ViewBag.WebsiteNames += list.Items[i].Name + "(" + list.Items[i].WebProperties[0].WebsiteUrl + ")";
            }

            return View("~/Views/Configboard.cshtml");
        }
        else
        {
            return new RedirectResult(result.RedirectUri);
        }
    }

PS: I have tried this sample code out in a ASP.NET MVC 5 project without Umbraco installed which works perfectly.

Any one able to push me into the right direction?

1

There are 1 best solutions below

0
On BEST ANSWER

For anyone else getting this problem, the solution was actually pretty simple:

I made a custom route for the AuthCallbackController (/authcallback/indexasync) and it all worked. Because Umbraco takes over the default routing this URL was not reachable hence the action of the authcallbackcontroller was not executed.