Esempio n. 1
0
        private static async void GetAPIToken(ApiControlViewModel.LoginViewModel model)
        {
            var    request         = HttpContext.Current.Request;
            var    tokenServiceUrl = request.Url.GetLeftPart(UriPartial.Authority) + request.ApplicationPath;
            string responseJson    = await ResponseAsStringAsync(string.Format("{0}/token", tokenServiceUrl),
                                                                 new[]
            {
                new KeyValuePair <string, string>("password", model.Password),
                new KeyValuePair <string, string>("username", model.UserName),
                new KeyValuePair <string, string>("grant_type", "password"),
            });

            var    jObject = JObject.Parse(responseJson);
            string token   = jObject.GetValue("access_token").ToString();
        }
Esempio n. 2
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));
            }
        }