How to fix Can't find Request.GetOwinContext in Web API
By FoxLearn 2/18/2024 1:33:47 AM 276
If you get an error can't get the UserManager from OwinContext in ApiController using ASP.NET MVC
You can fix the problem as below.
using System.Web; using System.Web.Http; using Microsoft.AspNet.Identity.Owin; using System.Linq; using System; using System.Threading.Tasks; using Microsoft.AspNet.Identity; namespace Invoice.Controllers { public class AccountController : ApiController { private ApplicationSignInManager _signInManager; private ApplicationUserManager _userManager; public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) { UserManager = userManager; SignInManager = signInManager; } public ApplicationSignInManager SignInManager { get { return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>(); } private set { _signInManager = value; } } public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } } } }
You should install Microsoft.AspNet.Identity.Owin from the Nuget Package Manager into your project, then just add the following code to the top.
using Microsoft.AspNet.Identity.Owin; using Microsoft.AspNet.Identity;
Remember replace HttpContext.GetOwinContext().Get<ApplicationSignInManager>() to HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>()
- Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager'
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- HTTP Error 500.30 ASP.NET Core app failed to start
- How to Use IExceptionHandler in ASP.NET Core
- How to custom exception handling in ASP.NET Core
- How to check if HttpPostedFileBase is an image
Categories
Popular Posts
Spica Admin Dashboard Template
11/18/2024