UserStore和Web API之间的安全漏洞

时间:2014-03-09 18:12:32

标签: c# asp.net-mvc asp.net-web-api asp.net-mvc-5

我正在尝试覆盖ASP.NET MVC 5项目的UserStore,因为我使用Web API 2来处理所有数据。

它工作正常但我的API中存在重大安全漏洞。我无法创建返回用户[autorize]的函数。所以任何人都可以使用任何用户名调用它并窃取所有用户的信息。

以下是UserController中的API函数。

        [Route("api/users/user/{username}")]
        public IHttpActionResult GetUserByUserName(string username)
        {
            username = HttpUtility.HtmlDecode(username);
            AspNetUser result = unitOfWork.UserRepository.Get(u => u.UserName == username).FirstOrDefault();

            //If the user doesn't exist
            if (result == null)
            {
                //throw NotFound("Could not find username: " + username);
                return NotFound();
            } 

            //Create the user to return
            User user = CreateUserFromEntity(result);
            return Ok(user);
        }

当用户尝试登录时,这一切都从这里开始。

此功能

await UserManager.FindAsync(model.UserName, model.Password);

调用我的UserStore以用户名获取用户,然后验证密码是否正常。然后,如果凭据良好且null出错,则返回用户。但我无法访问UserManager代码。

        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    await SignInAsync(user, model.RememberMe);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", Resources.Resources.InvalidCredentials);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

所以在我的UserStore我必须实现这个功能

我拥有的只是一个用户名,需要返回ApplicationUser。因此,客户端没有API的令牌,它会询问所有用户的信息。至少如果我有用户名和密码,我不介意API是不是[authorize]

        public Task<ApplicationUser> FindByNameAsync(string userName)
        {
            return Task<ApplicationUser>.Factory.StartNew(() =>
            {
                //Retrieves the API Url from the web config
                String uri = WebConfigurationManager.AppSettings["ApiURL"];

                //Set the api 
                Api api = new Api(uri);
                HttpResponseMessage response = api.HttpGet("api/users/user/" + HttpUtility.UrlPathEncode(userName));

                ApplicationUser ap = new ApplicationUser();
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body. Blocking!
                    var user = response.Content.ReadAsAsync<User>().Result;

                    //Set the application user with the response
                    ap.Id = user.Id;
                    ap.UserName = user.UserName;
                    ap.PasswordHash = user.Password;

                }
                else if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    //The username doesn't exist.
                    ap = null;
                }
                else
                {
                    throw new Exception("The response is not valid. Code: " + response.StatusCode + " Message: " + response.RequestMessage);
                }

                //Return the application user
                return ap;
            });
        }

我被困住了,我不知道应该如何处理这件事。我是否必须为客户端应用程序创建标识并使用其令牌调用API?我只是觉得有些不对劲。我认为最好将用户名和密码发送给API并让它管理凭据验证,但UserStore没有密码。

这样做的正确方法是什么?

谢谢

0 个答案:

没有答案