Ejemplo n.º 1
0
        public IHttpActionResult LoginUser(UserAccounts userInput) // LOGIN ACCOUNT
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            ;

            UserAccountsManager umgr = new UserAccountsManager();
            bool   loginOk           = umgr.Login(userInput.accountName, userInput.accountPassword);// Login function, returns bool
            string token             = umgr.CreateToken(userInput.accountName);

            if (token is null && loginOk.Equals(false))
            {
                return(Unauthorized());
            }

            if (loginOk.Equals(true)) // If bool is true, return Ok(200) http response

            {
                return(Ok(token));
            }

            return(Unauthorized()); // Returns if failed to verify account
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            UserAccountsManager _manager = new UserAccountsManager();
            UserDTO             user     = await _manager.Login(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim("role", user.Roles.ToString()));
            context.Validated(identity);
        }