コード例 #1
0
        /// <summary>
        /// Generate claims identity for token
        /// </summary>
        /// <param name="context">Context of request</param>
        /// <param name="user">Object user</param>
        /// <returns>Identity generated</returns>
        private Task GenerateJwt(OAuthGrantResourceOwnerCredentialsContext context, User user)
        {
            HttpResponseMessage responseMessage;
            IUser blUser = new BlUser();

            user.NumberOfAttemps = 0;
            blUser.UpdateUserAttemps(user, out responseMessage);
            blUser.SaveAuditLoginUser(user.Id);
            var identity = new ClaimsIdentity("JWT");

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim("UserName", context.UserName));
            identity.AddClaim(new Claim("Id", user.Id.ToString()));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "audience", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                {
                    "userName", context.UserName
                },
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
            return(Task.FromResult <object>(context));
        }
コード例 #2
0
        /// <summary>
        /// Update number attemps of login of a user
        /// </summary>
        /// <param name="context">Context of request</param>
        /// <param name="userLogin">Object user</param>
        /// <returns>Context message error</returns>
        private Task UpdateNumberAttempsUser(OAuthGrantResourceOwnerCredentialsContext context, User userLogin)
        {
            HttpResponseMessage responseMessage;
            IUser blUser = new BlUser();

            userLogin.NumberOfAttemps += 1;
            blUser.UpdateUserAttemps(userLogin, out responseMessage);

            context.SetError("ContraseñaIncorrecta", Resources.Message_es.IncorrectPassword);
            context.Response.Headers.Add(Constants.OwinChallengeFlag, new[] { ((int)HttpStatusCode.Unauthorized).ToString() }); //Little trick to get this to throw 401, refer to AuthenticationMiddleware for more
            return(Task.FromResult <object>(context));
        }
コード例 #3
0
        /// <summary>
        /// Validate the user in the database and generate the token
        /// </summary>
        /// <param name="context">context of the request</param>
        /// <returns>Token generated</returns>
        public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            try
            {
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); //Habilita CORS(Peticiones de origen cruzado) Para la generación del token.

                IUser blUser = new BlUser();

                if (!string.IsNullOrEmpty(context.UserName) &&
                    !string.IsNullOrEmpty(context.Password))
                {
                    User userLogin = blUser.GetUserByLogin(context.UserName, out responseMessage);

                    if (userLogin != null)
                    {
                        return(ValidateAndGenerate(context, ref responseMessage, blUser, userLogin));
                    }
                    else
                    {
                        Task messageUserNameNotFound = ResponseMessage(context, Resources.Message_es.NotFoundLogin, "UsuarioNoEncontrado");
                        return(messageUserNameNotFound);
                    }
                }
                else
                {
                    Task messageUserPasswordInvalid = ValidateUserPassword(context);
                    return(messageUserPasswordInvalid);
                }
            }
            catch (Exception ex)
            {
                ExceptionLogging.LogException(ex);
                responseMessage.Content      = new StringContent(Resources.Message_es.JWT);
                responseMessage.ReasonPhrase = ex.Message;
                responseMessage.StatusCode   = HttpStatusCode.Forbidden;
                return(Task.FromResult <object>(null));
            }
        }