예제 #1
0
        public LoginResponseModel LoginDemo(LoginTestBindingModel model)
        {
            //Admin admin = new Admin() {Admin_username=model.Username,Admin_pwd=model.Password };
            Admin admin    = _adminLog.GetByUsername(model.Username);
            int   validate = _adminLog.ValidateUser(model);
            ////MockAuthenticationService demoService = new MockAuthenticationService();
            ////Admin user = demoService.GetUser(model.Username, model.Password);
            LoginResponseModel loginresponse = new LoginResponseModel();

            if (validate == 0)
            {
                loginresponse.accesstoken = null;
                loginresponse.Admin_id    = -1;
                loginresponse.success     = false;
                //return N("Username or Password is Incorrect");
                return(loginresponse);//Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid Username or Password", Configuration.Formatters.JsonFormatter);
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(admin.Admin_username, admin.Admin_id);
                //string token = authentication.GenerateTokenForUser(model.sername, admin.Admin_id);
                loginresponse.accesstoken = token;
                loginresponse.Admin_id    = admin.Admin_id;
                loginresponse.success     = true;
                return(loginresponse); //Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter);
            }
        }
예제 #2
0
        public HttpResponseMessage PostSignIn([FromBody] User user)
        {
            IEnumerable <DBUser> usrs = db.Users.AsEnumerable().Where(u =>
                                                                      u.Email.Equals(user.Email) &&
                                                                      SecurePasswordHasher.Verify(user.Password, u.PassHash)
                                                                      );

            if (usrs.Count() == 0)
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User", Configuration.Formatters.JsonFormatter));
            }

            DBUser profile = usrs.First();

            if (profile == null)
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User", Configuration.Formatters.JsonFormatter));
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(profile.Email, profile.Id);
                return(Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter));
            }
        }
예제 #3
0
        public HttpResponseMessage Post()
        {
            var    httpContext = (HttpContextBase)Request.Properties["MS_HttpContext"];
            string username    = httpContext.Request.Form["username"];
            string password    = httpContext.Request.Form["password"];
            string email       = httpContext.Request.Form["email"];
            var    msg         = "";
            var    user        = mng.Users.CreateUser(username, email, password, out msg);

            if (user == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, msg, Configuration.Formatters.JsonFormatter));
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(user.userName, user.id);
                var    json  = JsonConvert.SerializeObject(
                    new
                {
                    id_token = token
                }
                    );
                return(Request.CreateResponse(HttpStatusCode.OK, json, Configuration.Formatters.JsonFormatter));
            }
        }
예제 #4
0
        public HttpResponseMessage Post()
        {
            var    httpContext = (HttpContextBase)Request.Properties["MS_HttpContext"];
            string email       = httpContext.Request.Form["email"];
            string password    = httpContext.Request.Form["password"];
            Dictionary <string, string> param;
            var user = mng.Users.GetUser(email, password, out param);

            if (user == null)
            {
                if (param["code"] == "400")
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, param["msg"], Configuration.Formatters.JsonFormatter));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, param["msg"], Configuration.Formatters.JsonFormatter));
                }
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(user.userName, user.id);
                var    json  = JsonConvert.SerializeObject(
                    new
                {
                    id_token = token
                }
                    );
                return(Request.CreateResponse(HttpStatusCode.OK, json, Configuration.Formatters.JsonFormatter));
            }
            //return Request.CreateResponse(HttpStatusCode.OK);
        }
예제 #5
0
        public static bool GenerateToken(LoginUserValidation user)
        {
            UserDetailsModel securityModel = new UserDetailsModel()
            {
                UserId   = user.UserId,
                UserName = user.UserName,
                RoleId   = user.RoleId
            };

            var isLogined = !string.IsNullOrEmpty(AuthenticationModule.GenerateTokenForUser(securityModel));

            return(isLogined);
        }
예제 #6
0
 public HttpResponseMessage LoginDemo([FromBody] User user)
 {
     if (user == null)
     {
         return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User", Configuration.Formatters.JsonFormatter));
     }
     else
     {
         AuthenticationModule authentication = new AuthenticationModule();
         string token = authentication.GenerateTokenForUser(user.Username, user.UserId);
         return(Request.CreateResponse(HttpStatusCode.OK, new { token = token, username = user.Username }, Configuration.Formatters.JsonFormatter));
     }
 }
        public IHttpActionResult Login(UserLoginDTO _user)
        {
            var user = _db.Users.Where(u => u.Username == _user.Username && u.Password == _user.Password).ToList();

            if (user != null && user.Count == 1)
            {
                AuthenticationModule auth = new AuthenticationModule();
                var token = auth.GenerateTokenForUser(user[0].Username, user[0].ID);

                return(Ok(token));
            }
            else
            {
                return(BadRequest());
            }
        }
예제 #8
0
        public HttpResponseMessage LoginDemo(LoginProfile loginProfile)
        {
            MockAuthenticationService demoService = new MockAuthenticationService();
            UserProfile user = demoService.GetUser(loginProfile.Username, loginProfile.Password);

            if (user == null)
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User", Configuration.Formatters.JsonFormatter));
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(user.UserName, user.UserId);
                return(Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter));
            }
        }
예제 #9
0
        public HttpResponseMessage Login(Login login)
        {
            var user = _repository.GetAllUsers()
                       .FirstOrDefault(u => u.UserName == login.UserName && u.Password == login.Password);

            if (user == null)
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User",
                                              Configuration.Formatters.JsonFormatter));
            }
            else
            {
                AuthenticationModule authentication = new AuthenticationModule();
                string token = authentication.GenerateTokenForUser(user.UserName, user.Id);
                return(Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter));
            }
        }
예제 #10
0
        public HttpResponseMessage Post(LoginModel loginModel)
        {
            try
            {
                UserManager objManager = new UserManager();
                var         user       = objManager.ValidateUser(loginModel.Username, loginModel.Password);

                if (user == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Incorrect credentials."));
                }

                var    authModule = new AuthenticationModule();
                string token      = authModule.GenerateTokenForUser(user);

                return(Request.CreateResponse(HttpStatusCode.OK, new { Token = token, Expires = DateTime.UtcNow.AddDays(30) }));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
예제 #11
0
        public HttpResponseMessage LoginDemo([FromBody] ApiControlViewModel.LoginViewModel context)
        {
            //var isCustomer=  HttpContext.Current.Request.Params["IsCustomer"];
            if (context.ClientId == "User")
            {
                PasswordHasher pass = new PasswordHasher();

                //var hashedPassword = EncodePassword(context.Password, MembershipPasswordFormat.Hashed, "MAKV2SPBNI99212");
                User user = new User();
                if (context.Email != null)
                {
                    user = db.Users.Where(x => x.Email == context.Email).FirstOrDefault();
                }
                if (context.UserName != null)
                {
                    user = db.Users.Where(x => x.UserName == context.UserName.Trim()).FirstOrDefault();
                }

                // password is correct


                //var userManager = context.OwinContext.GetUserManager<Loader.UserManager>();
                // var user = await userManager.FindAsync(context.UserName, context.Password);

                //Loader.Models.ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
                if (user != null && user.IsActive == true && pass.VerifyHashedPassword(user.PasswordHash, context.Password) != PasswordVerificationResult.Failed)
                {
                    if (user.UserDesignationId == 11)
                    {
                        var locations = String.Format("select  locationid from  fgetlocationlistbycollector('" + user.UserId + "')");


                        List <int> returnData = db.Database.SqlQuery <int>(locations).ToList();
                        int[]      myintlist  = returnData.ToArray();



                        AuthenticationModule authentication = new AuthenticationModule();
                        string tokens = authentication.GenerateTokenForUser(user.UserName, user.UserId);
                        //var sul = new LocationUser
                        //{
                        //EmployeeId = user.EmployeeId,
                        //Email = user.Email,
                        //UserId = user.UserId,
                        //UserName = user.UserName,
                        //EffDate = user.EffDate,
                        //TillDate = user.TillDate,
                        //MTId = user.MTId,
                        //IsUnlimited = user.IsUnlimited,
                        //UserDesignationId = user.UserDesignationId,
                        //Location = myintlist,
                        var token = tokens;
                        //};

                        return(Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter));
                        //return Ok(new { results = sul });
                    }
                    //else
                    //{
                    //    AuthenticationModule authentication = new AuthenticationModule();
                    //    string tokens = authentication.GenerateTokenForUser(user.UserName, user.UserId);
                    //    //var sul = new LocationUser
                    //    //{
                    //    //EmployeeId = user.EmployeeId,
                    //    //Email = user.Email,
                    //    //UserId = user.UserId,
                    //    //UserName = user.UserName,
                    //    //EffDate = user.EffDate,
                    //    //TillDate = user.TillDate,
                    //    //MTId = user.MTId,
                    //    //IsUnlimited = user.IsUnlimited,
                    //    //UserDesignationId = user.UserDesignationId,
                    //    var token = tokens;

                    //    //};
                    //    //return Ok(new { results = sul });

                    //    return Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter);
                    //}
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, "User is not collector ", Configuration.Formatters.JsonFormatter));
                    }
                }

                //Logger.writeLog(Request, Logger.JsonDataResult(sul), Logger.JsonDataResult(context));



                else if (user != null && user.IsActive == false)
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User", Configuration.Formatters.JsonFormatter));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "User Not Found ", Configuration.Formatters.JsonFormatter));
                }
            }

            else if (context.ClientId == "Customer")
            {
                PasswordHasher    pass = new PasswordHasher();
                CustomerUserTable user = new CustomerUserTable();
                if (context.UserName != null)
                {
                    user = db.CustomerUserTables.Where(x => x.UserName == context.UserName).FirstOrDefault();
                }
                if (context.Email != null)
                {
                    user = db.CustomerUserTables.Where(x => x.Email == context.Email).FirstOrDefault();
                }
                //var hashedPassword = EncodePassword(context.Password, MembershipPasswordFormat.Hashed, "MAKV2SPBNI99212");
                // var user = db.CustomerUserTables.Where(x => x.UserName == context.UserName).FirstOrDefault();

                //CustomerUser user = db.CustomerUsers.Where(x => x.UserName == context.UserName).FirstOrDefault();

                if (user != null && user.IsActive == true && pass.VerifyHashedPassword(user.PasswordHash, context.Password) != PasswordVerificationResult.Failed)
                {
                    AuthenticationModule authentication = new AuthenticationModule();
                    string tokens = authentication.GenerateTokenForUser(user.UserName, user.UserId);

                    //var sul = new customerUser
                    //{
                    //CustomerId = user.CustomerId,
                    //Email = user.Email,
                    //UserId = user.UserId,
                    //UserName = user.UserName,
                    //EffDate = user.EffDate,
                    //TillDate = user.TillDate,
                    //MTId = user.MTId,
                    //IsUnlimited = user.IsUnlimited,
                    var token = tokens;
                    //};
                    //Logger.writeLog(Request, Logger.JsonDataResult(sul), Logger.JsonDataResult(context));
                    //return Ok(new { results = sul });

                    return(Request.CreateResponse(HttpStatusCode.OK, token, Configuration.Formatters.JsonFormatter));
                }
                else if (user != null && user.IsActive == false)
                {
                    //return BadRequest("Customer Not Active");
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, "User Not Active", Configuration.Formatters.JsonFormatter));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "User Not Found", Configuration.Formatters.JsonFormatter));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "User Not Found", Configuration.Formatters.JsonFormatter));
            }
        }
예제 #12
0
        public HttpResponseMessage Login(LoginUserModel userLogin)
        {
            HttpRequestHeaders headers = Request.Headers;
            // var userName = string.Empty;
            //var password = string.Empty;

            //var userName = Request.GetHeader("userName");
            //if (headers.Contains("userName"))
            //{
            //    userName = headers.GetValues("userName").First();
            //}

            //if (headers.Contains("password"))
            //{
            //    password = headers.GetValues("password").First();
            //}

            UserSecurityModel userSecurity = _authenticationServices.GetUserByUserName(userLogin.Email);

            if (userSecurity != null)
            {
                AuthenticationFunctions authenticationFunctions = new AuthenticationFunctions();

                bool successful = authenticationFunctions.ValidatePassword(userLogin.Password, userSecurity.Password);

                if (!successful)
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid User or Password", Configuration.Formatters.JsonFormatter));
                }

                AuthenticationModule authentication = new AuthenticationModule();
                List <string>        roles          = new List <string>();
                foreach (RoleModel role in userSecurity.User.Roles)
                {
                    roles.Add(role.Name);
                }
                string token = authentication.GenerateTokenForUser(userSecurity, roles.ToArray());

                // Save the Security Token to the database for Audit purposes.
                _authenticationServices.UpdateLoginStatus(userSecurity.UserId, token, DateTime.Now.AddMinutes(int.Parse(InternalSettings.TokenExpirationMinutes)));

                AuthTokenModel returnToken = new AuthTokenModel()
                {
                    Token    = token,
                    Errors   = new List <string>(),
                    Messages = new List <string>()
                    {
                        "Success"
                    }
                };


                return(Request.CreateResponse(HttpStatusCode.OK, returnToken, Configuration.Formatters.JsonFormatter));
            }

            AuthTokenModel badReturnToken = new AuthTokenModel
            {
                Token  = "",
                Errors = new List <string>()
                {
                    "Invalid User Name or Password"
                },
                Messages = new List <string>()
                {
                    "Invalid Request or Missing Parameters"
                }
            };

            return(Request.CreateResponse(HttpStatusCode.BadRequest, badReturnToken));
        }