C# 如何统计对ASP.NET Web API POST方法的调用

C# 如何统计对ASP.NET Web API POST方法的调用,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,有没有办法计算Web API中对post方法的调用数 例如:我想禁用用户,如果他3次输入错误的用户名和密码组合。 所以在连续第三次调用WebAPI控制器的post方法之后,我想做一些事情(例如,我将以某种方式禁用用户或其他事情) 如何计算对API控制器方法的调用数?是否已经为这种情况定义了任何属性或方法 更新: 这是我的Web API方法: [Route("login")] public async Task<HttpResponseMessage> LoginUser(Login

有没有办法计算Web API中对post方法的调用数

例如:我想禁用用户,如果他3次输入错误的用户名和密码组合。 所以在连续第三次调用WebAPI控制器的post方法之后,我想做一些事情(例如,我将以某种方式禁用用户或其他事情)

如何计算对API控制器方法的调用数?是否已经为这种情况定义了任何属性或方法

更新:

这是我的Web API方法:

[Route("login")]
public async Task<HttpResponseMessage> LoginUser(Login model)
{
    using (AuthRepository repo = new AuthRepository())
    {
        Regex rgx = new Regex("[^a-zA-Z0-9 -]");
        string deviceId = rgx.Replace(model.DeviceId, "");
        var request = HttpContext.Current.Request;
        var user = await repo.FindUserAsync(deviceId, model.PIN);
        var tokenServiceUrl = request.Url.GetLeftPart(UriPartial.Authority) + request.ApplicationPath + "/Token";

        if (user != null)
        {
            MatrixLogManager.Debug("User " + model.DeviceId + "successfully logged in on MatrixSTS.");
            try
            {
                using (var client = new HttpClient())
                {
                    var requestParams = new List<KeyValuePair<string, string>>
                                                {
                                                    new KeyValuePair<string, string>("grant_type", "password"),
                                                    new KeyValuePair<string, string>("username", deviceId),
                                                    new KeyValuePair<string, string>("password", model.PIN)
                                                };

                    var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams);
                    var tokenServiceResponse = await client.PostAsync(tokenServiceUrl, requestParamsFormUrlEncoded);
                    var responseString = await tokenServiceResponse.Content.ReadAsStringAsync();
                    var responseCode = tokenServiceResponse.StatusCode;
                    var responseMsg = new HttpResponseMessage(responseCode)
                    {
                        Content = new StringContent(responseString, Encoding.UTF8, "application/json")
                    };

                    return responseMsg;
                }
            }
            catch (Exception ex)
            {
                MatrixLogManager.Error("Error: ", ex);
                throw ex;
            }
        }
        else
        {
                //IF LOGIN FAILD I WOULD NEED TO COUNT SOMEHOW THAT ONE CALL WAS UNSUCCESSFUL, AFTER THIRD I WILL BLOCK USER, BUT ONLY IT HE MAKES SAME MISTAKE 3 TIMES IN A ROW. 
               //Adding simple int counter didn't worked for me.
                return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid username or password.");
            }
        }
    }
[路由(“登录”)]
公共异步任务登录用户(登录模型)
{
使用(AuthRepository repo=new AuthRepository())
{
正则表达式rgx=新正则表达式(“[^a-zA-Z0-9-]”);
字符串deviceId=rgx.Replace(model.deviceId,“”);
var request=HttpContext.Current.request;
var user=await repo.FindUserAsync(deviceId,model.PIN);
var tokenServiceUrl=request.Url.GetLeftPart(UriPartial.Authority)+request.ApplicationPath+“/Token”;
如果(用户!=null)
{
MatrixLogManager.Debug(“用户”+model.DeviceId+“成功登录MatrixSTS”);
尝试
{
使用(var client=new HttpClient())
{
var requestParams=新列表
{
新的KeyValuePair(“授权类型”、“密码”),
新的KeyValuePair(“用户名”,设备ID),
新的KeyValuePair(“密码”,model.PIN)
};
var requestParamsFormUrlEncoded=新FormUrlEncodedContent(requestParams);
var tokenServiceResponse=Wait client.PostAsync(tokenServiceUrl,requestParamsFormUrlEncoded);
var responseString=await-tokenservicesponse.Content.ReadAsStringAsync();
var responseCode=tokenservicesponse.StatusCode;
var responseMsg=新的HttpResponseMessage(响应代码)
{
Content=newstringcontent(responseString,Encoding.UTF8,“application/json”)
};
返回响应;
}
}
捕获(例外情况除外)
{
MatrixLogManager.Error(“Error:,ex”);
掷骰子;
}
}
其他的
{
//若登录失败,我需要以某种方式计算一次呼叫失败,在第三次之后,我将阻止用户,但只有当他连续三次犯同样的错误时。
//添加简单的int计数器对我不起作用。
返回请求.CreateErrorResponse(HttpStatusCode.Unauthorized,“无效的用户名或密码”);
}
}
}

实现这一点的一个简单方法是保留一个静态的
字典
,它将用户映射到他们尝试登录的次数。注意,这可能会导致内存大量膨胀,具体取决于用户数量和您希望保持计数状态的时间:

private static readonly Dictionary<string, int> loginAttemptsByDeviceId = 
                                                new Dictionary<string, int>();
当然,在用户登录之前检查值:

int currentUserAttempts;
if (loginAttemptsByDeviceId.TryGetValue(deviceId, out currentUserAttempts) && 
                                        currentUserAttempts == MaxLoginThreshhold)
{
   // Return some error to the user.
}
这假定
DeviceID
是每个用户的唯一标识符。如果不是,请使用一个将唯一标识用户的值。
注意:我建议您长期保存这些数据(如果需要)。还请注意,这并不考虑可能尝试的并发请求。如果这是一个问题,请考虑使用<代码> CONTURNCE字典,并锁定在相关的地方。

< P>你应该把这个东西保存在DB的子表中,我们称之为UsRealDebug RealLogin:

该表将有一个userId的外键,并将有一个计数器值

当用户尝试登录时,首先检查用户有多少次失败,以及 如果他超过阈值,则在重置尝试次数之前,不要让他登录


如果用户登录失败,您将计数器增加1。

到目前为止您尝试过什么吗?@YuvalItzchakov我一直尝试使用简单的int计数器,但没有成功…它只计算第一次,下次调用值保持不变后…您能给我们看一下您的代码吗?@YuvalItzchakov当然,我会马上更新我的帖子…:)@YuvalItzchakov,所以这个方法有点不可读,但重点是我留下评论的部分。在循环的其他部分…我们对循环的if部分不感兴趣,因为只有用户成功登录时才会使用它。谢谢您的回答,我会立即尝试。。。我的目标是将此值保留在数据库中的某个位置,因为可能需要在其他一些调用中检查它:)@yuvalitzchakov@nemo_87然后创建将用户映射到其尝试的表,并在尝试登录用户之前查询该表。这将是一个更合适的长期方法。Hvala Amire:)我喜欢这种方法,也要尝试一下,因为我无论如何都需要将这个值保存在数据库中@阿米波维奇
int currentUserAttempts;
if (loginAttemptsByDeviceId.TryGetValue(deviceId, out currentUserAttempts) && 
                                        currentUserAttempts == MaxLoginThreshhold)
{
   // Return some error to the user.
}