public async Task <IHttpActionResult> UserRegistration(UserRegistration registration)
        {
            HttpResponseMessage response = null;

            if (registration != null && ModelState.IsValid)
            {
                var register = new UserLoginDTO()
                {
                    UserName = registration.UserName,
                    Email    = registration.Email,
                    Password = PassWordEncryption.EncryptPassword(registration.password),
                    Active   = true,
                    Locked   = false,
                    DateOfAccountCreation = DateTime.Now
                };

                // Register user
                userRegistration.RegisterUser(register);

                return(Ok(registration));// Request.CreateResponse(HttpStatusCode.OK, registration);
            }
            else
            {
                return(BadRequest(ModelState));
                //   response = Request.CreateResponse(HttpStatusCode.NotAcceptable, registration);
            }
            // return response;
        }
Esempio n. 2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var register = new UserLoginDTO()
            {
                UserName = context.UserName,
                Password = PassWordEncryption.EncryptPassword(context.Password),
                Active   = true,
                Locked   = false,
            };

            var user = userRegistration.AuthorisedUser(register);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
            //using (AuthRepository _repo = new AuthRepository())
            //{
            //    IdentityUser user = await _repo.FindUser(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"));//set user role later

            context.Validated(identity);
        }