Answer the question
In order to leave comments, you need to log in
Why does NullReference fail when trying to get AuthenticationManager via OwinContext?
Hello! When working with asp.net identity, I ran into a problem. Moved work with asp.net identity from asp.net mvc project (UI) to a separate project (BLL), created a field to get authenticationManager:
private IAuthenticationManager autenhticationManager => HttpContext.Current.GetOwinContext().Authentication;
2017/05/12 20:05:35.385 | fatal | System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.HttpContextExtensions.GetOwinEnvironment(HttpContext context)
at System.Web.HttpContextExtensions.GetOwinContext(HttpContext context)
at BusinessLogic.Service.UsersService.get_authenticationManager()
2017/05/12 20:05:35.479 | fatal | System.NullReferenceException: Object reference not set to an instance of an object.
at BusinessLogic.Service.UsersService.get_authenticationManager()
at BusinessLogic.Service.UsersService.d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
Answer the question
In order to leave comments, you need to log in
you need to configure Owin like this (depends on the implementation of the AuthenticationManager class):
[assembly: OwinStartup(typeof(YourAppName.App_Start.Startup))]
namespace YourAppName.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext<EF_DbContext>(EF_DbContext.Create);
...
app.CreatePerOwinContext<AuthenticationManager>(AuthenticationManager.Create);
...
}
}
}
public static AuthenticationManager Create(IdentityFactoryOptions<AuthenticationManager> options, IOwinContext context)
{
var manager = new AuthenticationManager(context.Get<EF_DbContext>());
return manager;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question