using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using log4net; using AIAHTML5.API.Constants; using AIAHTML5.API.Models; using System.Collections; namespace AIAHTML5.API.Controllers { public class AuthenticateController : ApiController { // GET api/authenticate public IEnumerable Get() { return new string[] { "value1", "value2" }; } // GET api/authenticate/5 public string Get(int id) { return "value"; } // POST api/authenticate public HttpResponseMessage Post([FromBody]JObject credentials) { ILog logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)); logger.Debug("inside POST"); dynamic authenticationRepsonse; //01. check user is authenticated or not by login credential macth bool isUserAuthenticated = AIAHTML5.API.Models.Users.IsUserAuthenticated(credentials); User objUser = new Models.User(); //02. Get User details objUser = AIAHTML5.API.Models.Users.getUserDetails(credentials); if(isUserAuthenticated) { //03.delete past wrong login attempts of user objUser.IsCorrectPassword = true; int wrongAttemptDeteledCount = AIAHTML5.API.Models.Users.deletePastWrongAttempts(objUser.Id); if (wrongAttemptDeteledCount < 0) { logger.Fatal("Unable to delete past wrong login attempts for userId= "+objUser.Id); } if (objUser.UserType == AIAHTML5.API.Models.User.SUPER_ADMIN || objUser.UserType == AIAHTML5.API.Models.User.GENERAL_ADMIN) { objUser.Modules = AIAHTML5.API.Models.Users.getAllModulesList(); AIAHTML5.API.Models.Users.insertLoginDetails(objUser.Id); } else { //03. get the license id for aUTHENTICATED USER objUser.LicenseId = AIAHTML5.API.Models.Users.getLicenseIdForThisUser(objUser.Id, "license"); objUser.EditionId = AIAHTML5.API.Models.Users.getLicenseIdForThisUser(objUser.Id, "edition"); //04.insert Log login details //AIAHTML5.API.Models.Users.insertLoginDetails(objUser.Id); //Commented above code inserts if the user license ~ subscription expired as well //05.Check user is active or not //objUser.IsActive = AIAHTML5.API.Models.Users.isUSerActive(objUser); //Id suggested but passed userInfo to avoid multiple database hitting //if (objUser.IsActive) //{ //Commenting as Inactive userid returns from here //5.1 check the License expiration //objUser.License.IsActive = AIAHTML5.API.Models.Users.isLicenseActive(objUser.LicenseId); objUser.License = AIAHTML5.API.Models.Users.getLicenseDetails(objUser.LicenseId); objUser.LicenseSubscriptions = AIAHTML5.API.Models.Users.getLicenseSubscriptionDetails(objUser.LicenseId); objUser.SubscriptionExpirationDateString = AIAHTML5.API.Models.Users.SubscriptionExpirationDateString(objUser.LicenseId); //5.2 Check for subscription Expiration [Promoted for case if license inactive along with subscription expired] if (!string.IsNullOrEmpty(objUser.SubscriptionExpirationDateString)) { objUser.IsSubscriptionExpired = true; } if (objUser.License.IsActive) { // send message to the UI for license expiration //5.2 Check for subscription Expiration //Insert user login details AIAHTML5.API.Models.Users.insertLoginDetails(objUser.Id); objUser.Modules = AIAHTML5.API.Models.Users.getModuleListByLicenseId(objUser.LicenseId); if (!objUser.License.IsTermAccepted) { ArrayList termsList = AIAHTML5.API.Models.Users.getTermsOfServiceText(); foreach (Hashtable item in termsList) { objUser.TermsOfServiceTitle = item["title"].ToString(); objUser.TermsOfServiceText = item["content"].ToString(); } } } //else //{ // //6. // // now return this list to the UI //} //} //else //{ // // send message back to th UI that user is inactive //} } } else { bool isCorrectLoginId = AIAHTML5.API.Models.Users.isCredentialCorrect(credentials, objUser, "loginId"); //bool isCorrectPassword = AIAHTML5.API.Models.Users.isCredentialCorrect(credentials, userInfo, "password"); if (!isCorrectLoginId) { objUser = null; } else { objUser.IsCorrectPassword = false; objUser.IncorrectLoginAttemptCount = AIAHTML5.API.Models.Users.checkNoOfWrongAttempts(objUser.Id) + 1; objUser.LoginFailureCauseId = ErrorHelper.E_PASSWORD_NOT_MATCH; //01. insert wrong attempt in dtabase if (objUser.IncorrectLoginAttemptCount == 1) { int insertedCount = AIAHTML5.API.Models.Users.insertWrongAttemptofUser(objUser.Id); } else { int updateCount = AIAHTML5.API.Models.Users.saveWrongAttemptofUser(objUser.Id); if (updateCount < 0) { //Put the log in log file logger.Fatal("Unable to Update past wrong login attempts for userId= " + objUser.Id); } else { //02. check no of wrong attempts //userInfo.IncorrectLoginAttemptCount = AIAHTML5.API.Models.Users.checkNoOfWrongAttempts(userInfo.Id); //if (userInfo.IncorrectLoginAttemptCount >= 5) //{ // userInfo.IsBlocked = true; // // send block message //} //else //{ // // send message back to UI for login fail //} if (objUser.IncorrectLoginAttemptCount > 4) { objUser.IsBlocked = true; objUser.LoginFailureCauseId = ErrorHelper.E_USER_ID_BLOCKED_24_HRS; } } } //if (objUser.License != null && !string.IsNullOrEmpty(objUser.License.AccountNumber)) //{ // int result = AIAHTML5.API.Models.Users.insertUserLoginLog(objUser.License.AccountNumber, objUser.LoginFailureCauseId, null, objUser.EditionId.ToString(), null); // if (result < 0) // logger.Fatal("Unable to insert wrong attempt detail in UserLoginLog table for accountNumber= " + objUser.License.AccountNumber); //} } } if(objUser!=null) authenticationRepsonse = JsonConvert.SerializeObject(objUser); else authenticationRepsonse = AIAConstants.USER_NOT_FOUND; //if (Convert.ToString(authenticationRepsonse) != AIAConstants.USER_NOT_FOUND && Convert.ToString(authenticationRepsonse) != AIAConstants.ERROR_IN_FECTHING_DETAILS && Convert.ToString(authenticationRepsonse)!= AIAConstants.SQL_CONNECTION_ERROR) //{ // //string userDetails = Newtonsoft.Json.JsonConvert.SerializeObject(authenticationRepsonse); // return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(authenticationRepsonse) }; //} //else //{ return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(authenticationRepsonse) }; //} } // PUT api/authenticate/5 public void Put(int id, [FromBody]string value) { } // DELETE api/authenticate/5 public void Delete(int id) { } } }