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.