I've built an ASP.NET WebAPI 2 application and one of my controllers have a method that allow users do login, something like this:
[Route("login")]
[HttpPost]
[SwaggerOperation(Tags = new[] { "ACCOUNT" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ReturnModel<AccountModel, HttpStatusCode>))]
public HttpResponseMessage Login([FromBody]LoginDto userData)
{
ReturnModel<AccountModel, HttpStatusCode> returnObj = new ReturnModel<AccountModel, HttpStatusCode>();
try
{
returnObj = accountService.Login(userData.UserLogin, userData.UserPassword);
if (returnObj.Success)
{
...
}
else
{
...
}
return Request.CreateResponse(returnObj.StatusCode, returnObj);
}
catch (Exception ex)
{
LogUtil.Error(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, returnObj);
}
}
LoginDto class:
public class LoginDto
{
[Required]
public string UserLogin { get; set; }
[Required]
public string UserPassword { get; set; }
}
After a vulnerability check with Checkmarx tool, i received a report that says:
Method Index at line 55 of //***/Controllers/AccountController.cs defines UserPassword, which is designated to contain user passwords. However, while plaintext passwords are later assigned to UserPassword, this variable is never cleared from memory.
I know that strings are immutable and stays in memory for an undetermined time.
The tool (checkmarx) have a suggestion that says:
Specific Recommendations - .NET:
- Instead of storing passwords in immutable strings, prefer to use an encrypted memory object, such as SecureString or ProtectedData.
I found other suggestions that also say me to use SecureString instead string to manipulate/store user passwords, but i think that this only applies to MVC or WPF applications, i can't figure out how can i apply this on an ASP.NET WebAPI 2 context, where i have to expose a language agnostic service.
Anyone have any suggestion about how can i achieve this level of security (or even if it's possible) on an ASP.NET WebAPI 2?