Beispiel #1
0
 public HttpResponseMessage Post([FromBody] LoginAPIModel user)
 {
     try
     {
         if (ModelState.IsValid)
         {
             UserAPIModel Luser = mapper.Map <UserAPIModel>(userBDC.AuthenticateUser(user.UserName, user.Password));
             if (Luser != null)
             {
                 return(Request.CreateResponse(HttpStatusCode.OK, Luser));
             }
             else
             {
                 return(Request.CreateResponse(HttpStatusCode.BadRequest, Resources.AlreadyRegisteredUser));
             }
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
         }
     }
     catch (Exception)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, false));
     }
 }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var authUser = userBDC.AuthenticateUser(context.UserName, context.Password);

            if (authUser != null)
            {
                UserAPIModel currentUser = mapper.Map <UserAPIModel>(authUser);
                identity.AddClaim(new Claim(Resources.Id, Convert.ToString(currentUser.IsApprover)));
                identity.AddClaim(new Claim(Resources.Status, Convert.ToString(currentUser.CurrentStatus)));
                identity.AddClaim(new Claim(Resources.Id, Convert.ToString(currentUser.ID)));
                identity.AddClaim(new Claim(Resources.Email, Convert.ToString(currentUser.Email)));
                identity.AddClaim(new Claim(Resources.Fname, Convert.ToString(currentUser.FirstMidName)));
                identity.AddClaim(new Claim(ClaimTypes.Role, Convert.ToString(currentUser.IsApprover)));
                var props = new AuthenticationProperties(new Dictionary <string, string>
                {
                    {
                        Resources.Id, Convert.ToString(currentUser.ID)
                    },
                    {
                        Resources.Email, context.UserName
                    },
                    {
                        Resources.Role, Convert.ToString(currentUser.IsApprover)
                    },
                    {
                        Resources.Status, Convert.ToString(currentUser.CurrentStatus)
                    },
                    {
                        Resources.Fname, currentUser.FirstMidName
                    },
                    {
                        Resources.ProfilePic, currentUser.ProfileImage
                    }
                });
                var ticket = new AuthenticationTicket(identity, props);
                context.Validated(ticket);
                //else
                //{
                //    //context.SetError("invalid_grant", "Provided username and password is not matching, Please retry!");
                //    //context.Rejected();
                //}
            }
            else
            {
                context.SetError(Resources.InvalidGrant, Resources.InvalidCredentials);
                //context.Rejected();
            }
            return;
        }