Ser*_*diy 10 .net asp.net-mvc dependency-injection owin asp.net-identity
我正在向ASP.NET MVC 5 Web应用程序添加ASP.NET身份验证功能.
我在整个项目中使用Unity进行依赖注入,所以我决定注入AccountController构造函数中所需的依赖项:
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser, string> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
Run Code Online (Sandbox Code Playgroud)
我的Login方法实现如下(实际上,我从ASP.NET Web Application具有Individual User Accounts身份验证的项目模板中复制了该代码):
//
// 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 and return appropriate view...
// However, there are no authentication cookies in the response!
}
Run Code Online (Sandbox Code Playgroud)
问题是,认证工作不正常-即使我进入了正确的凭据,result是SignInStatus.Success有在响应不发送身份验证cookie.
但是,如果我使用OWIN基础结构来解析ApplicationSignInManager而不是Unity容器,一切都正常:
//
// 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 and return appropriate view...
// Authentication cookies are present in the response!
}
Run Code Online (Sandbox Code Playgroud)
这ApplicationSignInManager就是在应用程序Startup类中注册的方式:
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
Run Code Online (Sandbox Code Playgroud)
这是ApplicationSignInManager声明:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Unity配置的一部分:
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));
Run Code Online (Sandbox Code Playgroud)
这个想法是Unity ApplicationSignInManager为Create方法提供了与构造函数相同的依赖关系.但Unity的方法由于某种原因不起作用:成功登录后不会发送身份验证cookie.
这是一个非常具体的问题,但也许有人之前遇到过这样的问题?我相信这种行为应该与OWIN中间件,管道以及在应用程序启动时如何连接所有这些东西有关.
tra*_*max 13
而不是IOwinContext在容器中注册,请注册IAuthenticationManager:
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
Run Code Online (Sandbox Code Playgroud)
并且只有一个SignInManager构造函数:
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4101 次 |
| 最近记录: |