.net - Injecting SignInManager dependency: does not work with Unity, works when using OWIN -
i adding asp.net identity authentication functionality asp.net mvc 5 web application.
i using unity dependency injection across project, decided inject dependencies required accountcontroller
in constructor:
public accountcontroller(usermanager<applicationuser> usermanager, signinmanager<applicationuser, string> signinmanager) { _usermanager = usermanager; _signinmanager = signinmanager; }
my login
method implemented following (actually, copied code asp.net web application
project template individual user accounts
authentication):
// // post: /account/login [httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> login(loginviewmodel model, string returnurl) { var result = await _signinmanager.passwordsigninasync(model.email, model.password, model.rememberme, shouldlockout: true); // process result , return appropriate view... // however, there no authentication cookies in response! }
the problem authentication not work correctly - if entered correct credentials , result
signinstatus.success
, there no authentication cookies being sent in response.
however, if use owin
infrastructure resolve applicationsigninmanager
instead of unity container, works correctly:
// // post: /account/login [httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> login(loginviewmodel model, string returnurl) { var owinsigninmanager = httpcontext.getowincontext().get<applicationsigninmanager>(); var result = await owinsigninmanager.passwordsigninasync(model.email, model.password, model.rememberme, shouldlockout: true); // process result , return appropriate view... // authentication cookies present in response! }
that's how applicationsigninmanager
registered in application startup
class:
app.createperowincontext<applicationsigninmanager>(applicationsigninmanager.create);
and applicationsigninmanager
declaration:
public class applicationsigninmanager : signinmanager<applicationuser, string> { public applicationsigninmanager(applicationusermanager usermanager, iauthenticationmanager authenticationmanager) : base(usermanager, authenticationmanager) { } public static applicationsigninmanager create(identityfactoryoptions<applicationsigninmanager> options, iowincontext context) { var usermanager = new applicationusermanager(new userstore<applicationuser>(new databasecontext())); return new applicationsigninmanager(usermanager, context.authentication); } }
here's part of unity configuration:
unitycontainer.registertype<httpcontextbase>(new injectionfactory(c => new httpcontextwrapper(httpcontext.current))); unitycontainer.registertype<iowincontext>(new injectionfactory(c => c.resolve<httpcontextbase>().getowincontext())); unitycontainer.registertype<iauthenticationmanager>(new injectionfactory(c => c.resolve<iowincontext>().authentication));
the idea unity provides same dependencies applicationsigninmanager
constructor create
method does. unity's approach not work reason: no authentication cookies being sent after successful log in.
this specific question, maybe faced problem before? believe behavior should related owin middleware, pipeline , how stuff being wired @ application startup.
instead of registering iowincontext
in container, register iauthenticationmanager:
container.registertype<iauthenticationmanager>( new injectionfactory(c => httpcontext.current.getowincontext().authentication));
and have 1 constructor signinmanager:
public applicationsigninmanager(applicationusermanager usermanager, iauthenticationmanager authenticationmanager)
i have done registration unity like this, , here the explanation.
Comments
Post a Comment