Search code examples
c#asp.net-mvcasp.net-mvc-5windows-servicesasp.net-identity-2

How to share MVC 5 Identity 2.0 authentication (esp SignInManager) with windows service application?


I have developed an MVC 5 application that uses Identity 2.0 to authenticate users. It works well.

I have a windows service that provides specialized services for web users. Web users make their requests to the service, which runs on the web server in parallel with IIS), through MSMQ. The service responds asynchronously through MSMQ when the original request completes.

The service does not currently use HTTP and does not have HttpContext, which means I cannot use the same approach to Owin as I do in the MVC 5 web application.

I need the windows service to authenticate through the same Identity 2.0 scheme that the users have, and plan to put a static username and password into the app.config file of the service. However, I cannot figure out how to instantiate the Owin components so I can get to UserManager and authenticate the user.

UPDATE: I figured out a way to get the UserManager without OWIN (see following) using classes from the default Identity 2.0 spec.

using Microsoft.AspNet.Identity;

<snip>

    var userManager = new ApplicationUserManager(new ApplicationUserStore(new ApplicationDbContext()));

Now I am stuck trying to create SignInManager as it has a direct dependence on OWIN in the constructor. All suggestions appreciated.

Does anyone have an example of how to do this?


Solution

  • SignInManager works with OWIN to set an authentication cookie on HTTP reply. If you say your service is not running HTTP (unless I misunderstand your question), then there is no point in trying using SignInManager because there is nowhere to set a cookie to. I suggest you review how and what you need from Identity and how you authenticate your service