Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MVC Identity code from desktop application

I'm trying to use the MVC Identity code from a desktop application. The desktop application needs to make a bunch of additions and updates to my user data.

I have copied the classes over from a generated MVC application, installed the required packages and made all changes necessary for the code to compile.

The only problem I have now is creating an instance of the ApplicationUserManager class.

public ApplicationUserManager UserManager
{
    get => _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    private set
    {
        _userManager = value;
    }
}
private ApplicationUserManager _userManager;

The problem is I have no HttpContext, and so HttpContext.Current will always return null.

Is what I'm doing possible? How can I build a UserManager from a desktop application without an HTTP context?

UPDATE:

I have direct access to the database, so I'd be happy with a solution that didn't require the Identity code and classes. The biggest hurdle here would be creating and updating passwords so that they can be "understood" by the Identity code in my website app.

like image 761
Jonathan Wood Avatar asked Feb 12 '19 21:02

Jonathan Wood


People also ask

Can we use MVC in desktop applications?

MVC is UI platform agnostic. You can do web, voice, desktop or console apps.


1 Answers

interesting approach,

otherwise, it will be necessary to clean out everything that httpcontext touches..

you can create ApplicationUserManager instance like this.

public ApplicationUserManager UserManager
{
    get
    { 
        if(_userManager == null)
        {
            _userManager =  new ApplicationUserManager(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(yourDbContext));
        }
        return _userManager;
    }
}
like image 66
levent Avatar answered Oct 25 '22 19:10

levent