2016-01-26 25 views
5

Tôi có dịch vụ Webapi tạo mật khẩu Đặt lại mật khẩu.
Mã điểm dịch vụ thế hệ cuối:ASP.NET Identity WebAPI mã thông báo đặt lại mật khẩu không hợp lệ

[Authorize(Users = "abcd")] 
    [HttpGet] 
    [ActionName("GenerateForgotPasswordToken")] 
    public async Task<IHttpActionResult> GenerateForgotPasswordToken(string key) 
    { 
     if (key == null || UserManager.FindById(key) == null) 
     { 
      return InternalServerError(new Exception("User not found")); 
     } 
     return Ok(await UserManager.GeneratePasswordResetTokenAsync(key)); 
    } 

Ứng dụng của tôi UserManager:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     //var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
     var manager = new ApplicationUserManager(new UserStore<IdentityUser>(new MTA())); 
     // Configure validation logic for usernames 
     manager.UserValidator = new UserValidator<IdentityUser>(manager) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 
     // Configure validation logic for passwords 
     manager.PasswordValidator = new PasswordValidator 
     { 
      RequiredLength = 6, 
      RequireNonLetterOrDigit = true, 
      RequireDigit = true, 
      RequireLowercase = true, 
      RequireUppercase = true, 
     }; 
     var dataProtectionProvider = options.DataProtectionProvider; 
     if (dataProtectionProvider != null) 
     { 
      manager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
     } 
     return manager; 
    } 

Các thẻ sẽ được sử dụng trong một email để gửi một URL khôi phục mật khẩu cho người dùng. URL đó trỏ đến một khung nhìn ASP.NET MVC là một phần của dự án WebAPI của tôi và rõ ràng được lưu trữ trong cùng một ứng dụng web trong IIS. Nút đặt lại mật khẩu trên trang gọi cho điểm cuối dịch vụ khác của tôi để đặt lại mật khẩu.
Passowrd điểm reset dịch vụ cuối:

[HttpGet] 
    [AllowAnonymous] 
    public async Task<HttpResponseMessage> ResetPassword([FromUri]string email,[FromUri]string code,[FromUri]string password) 
    { 
     var user = await UserManager.FindByEmailAsync(email); 
     var result = await UserManager.ResetPasswordAsync(user.Id, code, password); 
     if (result.Succeeded) 
     { 
      return Request.CreateResponse(); 
     } 
     return Request.CreateResponse(System.Net.HttpStatusCode.Ambiguous); 
    } 

Ngoài ra nó có thể giúp đề cập đến cả những điểm cuối web api là trong bộ điều khiển tương tự và bộ điều khiển tôi xác định một UserManger toàn cầu như sau:

private ApplicationUserManager _userManager; 
    public ApplicationUserManager UserManager 
    { 
     get 
     { 
      return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     } 
     private set 
     { 
      _userManager = value; 
     } 
    } 

Khi tôi sử dụng công cụ bên ngoài như Advanced REST Client, tôi có thể nhấn điểm cuối đầu tiên để tạo mã thông báo và sau đó chuyển mã thông báo đến điểm cuối thứ hai cùng với email và mật khẩu mới và đặt lại mật khẩu thành công. Tuy nhiên khi bộ điều khiển ASP.NET MVC của tôi sử dụng cùng một mã thông báo được tạo bởi điểm cuối đầu tiên và gọi điểm cuối passwordReset, mã thông báo không hợp lệ! Tôi đã đảm bảo rằng không có vấn đề Mã hóa/Giải mã và mã thông báo nhận được bởi điểm kết thúc thứ hai giống hệt nhau trong cả hai bài kiểm tra.
Một lần nữa tất cả các bộ điều khiển WebApi và ASP của tôi nằm trong cùng một dự án và được lưu trữ trong cùng một ứng dụng web. Tôi nghĩ rằng sự cố có thể liên quan đến việc có một nhà cung cấp dịch vụ Mã số khi yêu cầu mới bắt đầu dựa trên OwinContext nhưng tôi không hiểu tại sao nó hoạt động gọi nó thông qua trình duyệt web.

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 

Trả lời

1

tôi thấy vấn đề sau khi đọc câu hỏi này: Make ASP.NET Identity 2.0 Email confirm token work for WCF and MVC

Trong trường hợp của tôi IIS đã được cấu hình để tạo ra Máy chính trong thời gian chạy và đó là lý do tại sao thẻ không hợp lệ ngay cả khi tất cả các mã của tôi là chạy như một ứng dụng duy nhất. Automatically generate at runtime should be unchecked

Đây là hướng dẫn về cấu hình khóa máy IIS: How to Generate Machine Key using IIS

Các vấn đề liên quan