public HttpResponseMessage Login() { var authenticationToken = Request.Headers.Authorization.Parameter; var decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken)); var usernamePasswordArray = decodedAuthenticationToken.Split(':'); var userName = usernamePasswordArray[0]; var password = usernamePasswordArray[1]; var IP = Request.Headers.GetValues("IP").First(); Login login = new Login() { UserName = userName, Password = password }; Users LoginUser = UBL.Login(login); if (LoginUser.UserID > 0) { GeolocationStack location = GetGeolocation(IP); LoginRecord loginRecord = new LoginRecord() { UserID = LoginUser.UserID, IP = location.Ip, Country = location.CountryName, Region = location.RegionName, City = location.City }; UBL.AddLogin(loginRecord); Users Details = UBL.Details(LoginUser.UserID); //Details.RolesData = RBL.List().Where(x => x.RoleID == Details.RoleID).FirstOrDefault(); //Details.GroupList = GBL.ListbyUser(Details.UserID); var token = GenerateToken(LoginUser.UserID); if (token.TokenID.Length > 0) { Details.NeedResetPwd = LoginUser.NeedResetPwd; Details.Token = token.TokenID; Details.TokenExpires = token.ExpiresDate; Details.TokenExpiresMin = expireTime; return(this.Request.CreateResponse(HttpStatusCode.OK, Details)); } else { return(this.Request.CreateResponse(HttpStatusCode.InternalServerError)); } } else { return(this.Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
static GeolocationStack GetGeolocation(string IP) { string url = API_URL + IP + $"?access_key={API_KEY}"; string resultData = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { resultData = reader.ReadToEnd(); } GeolocationStack location = JsonConvert.DeserializeObject <GeolocationStack>(resultData); return(location); }
public HttpResponseMessage AddNew([FromBody] Contacts Detail) { GeolocationStack location = GetGeolocation(Detail.IP); Detail.Country = location.CountryName; Detail.Region = location.RegionName; Detail.City = location.City; var r = CBL.Add(Detail); if (!r) { return(this.Request.CreateResponse(HttpStatusCode.InternalServerError)); } else { return(this.Request.CreateResponse(HttpStatusCode.OK, r)); } }