1

I am using a MVC application (https://github.com/microsoftgraph/aspnet-webhooks-rest-sample) where a user signs in to their Microsoft account and creates a subscription to a Graph webhook in order to monitor for emails. The application works fine, but I am integrating this into a larger application where the user will not sign in, but rather needs to be subscribed to the Graph webhook when they launch the application.

My question is this: currently, the user clicks on "sign in" which invokes the SignIn() controller action. How do I call the SignIn part of the controller action without going through the view?

Relevant portions of code is below:

Login.cshtml

<li class="nav-item">
        @Html.ActionLink("Sign In", "SignIn", "Account", new { area = "" }, new { @class = "nav-link" })
</li>

AccountController.cs

[HttpGet]
public void SignIn()
{
    if (HttpContext.User == null || !HttpContext.User.Identity.IsAuthenticated)
    {
         HttpContext.GetOwinContext().Authentication.Challenge(
             new AuthenticationProperties { RedirectUri = "/" },
                OpenIdConnectAuthenticationDefaults.AuthenticationType);
    }
}

I tried calling the controller from Global.asax.cs and from Startup.cs (as mentioned here, here and here). However, application fails due to HttpContext being null. I tried circumventing this by adding following code in AccountController.cs and calling it from Startup.cs, but app still fails. How can I invoke SignIn() with a valid HttpContext and without user clicking on any button?

public void Triage()
{
    AccountController acntController = new AccountController();
    acntController.ControllerContext = new ControllerContext(
        this.ControllerContext.RequestContext,
            acntController
    );
    acntController.SignIn();
}

EDIT 1: After trying out various approaches, I feel the one mentioned above may not be right. I will post a different question. If anything changes, I will edit the question. Thanks.

Sarvavyapi
  • 810
  • 3
  • 23
  • 35
  • 2
    Abstract the common code to a new method and have both actions call that. You might even create another class or move the code to an other existing class. – Igor Oct 23 '18 at 09:53
  • if you need actions to be called unanimously (without signing in) you can do this by removing Authorize attribute from top of actions in controller. no need to call signin method manually. – VahiD Oct 23 '18 at 09:56
  • @Igor - I tried doing that, however, "this.ControllerContext" is null and so HttpContext is also null. Can you give me an example? I tried doing something like what is mentioned here - https://stackoverflow.com/questions/19055119/controllercontext-is-null – Sarvavyapi Oct 23 '18 at 10:39
  • @VahiD - there is no Authorize attribute on top of actions – Sarvavyapi Oct 23 '18 at 10:39
  • yes you can authorize actions, see [this](https://stackoverflow.com/questions/16709853/asp-net-mvc4-authorize-on-both-controller-and-action) – Pio Oct 23 '18 at 11:04
  • so please share the code of other actions as well, the ones that you need to call them without signing in – VahiD Oct 23 '18 at 18:07
  • After trying out various approaches, I feel the one mentioned above is not right. I will post a different questions. Thanks. – Sarvavyapi Oct 29 '18 at 14:49

0 Answers0