/// <summary> /// Methode qui sert à la vérification de l'authentification du client /// </summary> /// <param name="context">Le context de la requête et d'autre information utiles à la gestions du service</param> /// <returns></returns> public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { IDependencyScope Scope = context.OwinContext.Get <IDependencyScope>(); ClientUserManager manager = Scope.GetService(typeof(ClientUserManager)) as ClientUserManager; ClientRoleManager managerRoles = Scope.GetService(typeof(ClientRoleManager)) as ClientRoleManager; /*ClientUserManager manager = context.OwinContext.GetUserManager<ClientUserManager>(); * ClientRoleManager managerRoles = context.OwinContext.Get<ClientRoleManager>();*/ var AllowOriginCORS = context.OwinContext.Get <string>(GenericNames.OWIN_CONTEXT_CORS); var attempt = Convert.ToInt32(ConfigurationManager.AppSettings[GenericNames.CAPTCHA_FAILED_COUNT]); int RefreshTokenLifeTime = context.OwinContext.Get <int>(GenericNames.OWIN_CONTEXT_REFRESH_TOKEN_LIFETIME); if (AllowOriginCORS == null) { AllowOriginCORS = "*"; } context.OwinContext.Response.Headers.Remove(GenericNames.OWIN_CONTEXT_CORS_HEADER); context.OwinContext.Response.Headers.Add(GenericNames.OWIN_CONTEXT_CORS_HEADER, new[] { AllowOriginCORS }); Client user = await manager.FindAsync(context.UserName, context.Password); if (user == null) { Client FindByEmail = await manager.FindByEmailAsync(context.UserName); if (FindByEmail != null) { FindByEmail.AccessFailedCount++; FindByEmail.LastAttemptConnexion = DateTime.UtcNow; await manager.UpdateAsync(FindByEmail); if (FindByEmail.AccessFailedCount > attempt) { context.SetError("captcha", GenericError.NEED_CAPTCHA); return; } } context.SetError("invalid_grant", GenericError.INVALID_GIVEN_PARAMETER); return; } if (!user.EmailConfirmed) { context.SetError("email_confirmation", GenericError.EMAIL_NOT_CONFIRMED); return; } if (user.LockoutEnabled) { context.SetError("client", GenericError.ACCOUNT_DISABLED); return; } AuthenticationTicket ticket = null; if (user.AccessFailedCount > attempt) { var data = await context.Request.ReadFormAsync(); var Code = data[GenericNames.GOOGLE_RECAPTCHA_FORM]; if (Code == null) { context.SetError("captcha", GenericError.CAPTCHA_MISSING_RESPONSE); return; } else { ICaptchaTools tools = GoogleReCaptchValidator.Create(); var testCaptcha = await tools.VerifyCaptcha(Code, context.Request.RemoteIpAddress); if (testCaptcha) { user.AccessFailedCount = 0; await manager.UpdateAsync(user); ticket = AuthenticationTools.GenerateTicket(context.Options.AuthenticationType, context.ClientId, user, RefreshTokenLifeTime); } else { context.SetError("captcha", GenericError.CAPTCHA_INVALID_SOLUTION); return; } } } else { ticket = AuthenticationTools.GenerateTicket(context.Options.AuthenticationType, context.ClientId, user, RefreshTokenLifeTime); } context.Validated(ticket); }