我有一个奇怪的问题,我正在服务器中使用AngularJs和Web API构建单页应用程序,我正在使用Entity框架,我使用Code First方法一切顺利,直到我想实现更改密码对于用户来说,更新是正确的,但是当用户尝试重新连接他的新凭据时,实体框架会收集旧密码!!
public class AuthenticationFilter : ActionFilterAttribute
{
private MyDbRepository repo;
public KhbyraAuthenticationFilter()
{
repo = new MyDbRepository(new MyDbContext());
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
//Login code accessing database by repo object !!
//Here where Entity framework gather old info
}
}
这是SecurityController中的登录操作
[EnableCors("*", "*", "*")]
public class SecurityController : BaseApiController
{
//other actions
[AuthenticationFilter]
[Route("Token")]
[HttpPost]
public IHttpActionResult Login([FromBody]User user)
{
if (user == null)
{
Unauthorized();
}
return Ok();
}
}
修改
这是变更传递的地方
[EnableCors("*", "*", "*")]
[KhbyraAuthorizeAttribute]
public class UserController : BaseApiController
{
private int CurrentUserID;
public UserController():base(new KhbyraRepository(new KhbyraContext()))
{
}
//.. other actions
//..
[Route("User/ChangePassword")]
[HttpPost]
public IHttpActionResult ChangePassword([FromBody]ChangePasswordModel model)
{
// here where i save the new password
}
答案 0 :(得分:1)
您必须在OnActionExecuting
中的AuthenticationFilter
方法中实例化新的存储库。过滤器是一个单例,因此您保留一个具有旧值缓存的DbContext
实例。
public class AuthenticationFilter : ActionFilterAttribute
{
public KhbyraAuthenticationFilter()
{
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
using(var repo = new MyDbRepository(new MyDbContext()))
{
//Login code accessing database by repo object.
}
}
}
这也使代码线程安全(当前不是)。