public IActionResult GenerateNewApiToken([FromRoute] string ApiKey, [FromBody] UserObj obj) { IActionResult response = Unauthorized(); UserApiTokenResponse resp = new UserApiTokenResponse(); try { UserJwt uj = GenerateJSONWebToken(); int ReturnVal; string ReturnMsg; UserApiTokenObj utj = new UserApiTokenObj(); utj.user_id = obj.user_id; utj.api_token = uj.token_String; utj.token_expiry = uj.expiry; SetUserApiToken(ApiKey, utj, out ReturnVal, out ReturnMsg); if (ReturnVal == 1) { resp.statuscode = (int)Common.ResponseStatusCode.Success; resp.message = "success"; resp.api_token = uj.token_String; response = Ok(resp); } else { resp.statuscode = (int)Common.ResponseStatusCode.SqlException; resp.message = ReturnMsg; response = Conflict(resp); } } catch (Exception ex) { Common c = new Common(); ExceptionObj exobj = c.GetExceptionObjBase(ex); exobj.form_name = "AuthentcationController"; exobj.page_url = "api/Authentication/GenerateNewApiToken"; int ReturnVal; string ReturnMsg; ExceptionDAO exd = new ExceptionDAO(_ConStr); exd.SetExceptionLog(ApiKey, exobj, out ReturnVal, out ReturnMsg); resp.statuscode = (int)Common.ResponseStatusCode.Exception; resp.message = ex.Message.ToString(); response = BadRequest(resp); } return(response); }
public void SetUserApiToken(string apikey, UserApiTokenObj obj, out int ReturnVal, out string ReturnMsg) { try { List <DBParameter> pList = new List <DBParameter>(); pList.Add(new DBParameter("user_id", SqlDbType.Int, 0, ParameterDirection.Input, obj.user_id)); pList.Add(new DBParameter("api_token", SqlDbType.VarChar, 1000, ParameterDirection.Input, obj.api_token)); pList.Add(new DBParameter("token_expiry", SqlDbType.VarChar, 50000, ParameterDirection.Input, obj.token_expiry)); pList.Add(new DBParameter("ReturnVal", SqlDbType.Int, 0, ParameterDirection.Output, 0)); pList.Add(new DBParameter("ReturnMsg", SqlDbType.VarChar, 50000, ParameterDirection.Output, "")); objDAO = new DAO(_constr); objDAO.ExecuteNonQuery("dbo.Set_USER_Api_Token", DAO.QueryType.StoredProcedure, pList, out ReturnVal, out ReturnMsg); } catch (Exception) { throw; } }
public IActionResult AuthorizationToken([FromRoute] string ApiKey, [FromBody] LoginObj obj) { IActionResult response = Unauthorized(); UserAuthenticationResponse resp = new UserAuthenticationResponse(); try { DataSet ds; int ReturnVal; string ReturnMsg; var IsAuth = AuthenticateApiCaller(ApiKey, obj, out ds, out ReturnVal, out ReturnMsg); if (IsAuth) { var tokenString = ""; if (String.IsNullOrEmpty(ds.Tables[0].Rows[0]["active_api_token"].ToString())) { UserJwt uj = GenerateJSONWebToken(); tokenString = uj.token_String; int ReturnVal_utj; string ReturnMsg_utj; UserApiTokenObj utj = new UserApiTokenObj(); utj.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString()); utj.api_token = uj.token_String; utj.token_expiry = uj.expiry; SetUserApiToken(ApiKey, utj, out ReturnVal_utj, out ReturnMsg_utj); if (ReturnVal_utj != 1) { resp.statuscode = (int)Common.ResponseStatusCode.SqlException; resp.message = ReturnMsg_utj; response = Conflict(resp); return(response); } } else { tokenString = ds.Tables[0].Rows[0]["active_api_token"].ToString(); } resp.statuscode = (int)Common.ResponseStatusCode.Success; resp.message = "success"; resp.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString()); resp.studio_id = int.Parse(ds.Tables[0].Rows[0]["studio_id"].ToString()); resp.full_name = ds.Tables[0].Rows[0]["full_name"].ToString(); resp.api_key = ds.Tables[0].Rows[0]["api_key"].ToString(); resp.api_token = tokenString; response = Ok(resp); } else { resp.statuscode = (int)Common.ResponseStatusCode.ValidationException; resp.message = ReturnMsg; response = Unauthorized(resp); } } catch (Exception ex) { Common c = new Common(); ExceptionObj exobj = c.GetExceptionObjBase(ex); exobj.form_name = "AuthentcationController"; exobj.page_url = "api/Authentication/AuthorizationToken"; int ReturnVal; string ReturnMsg; ExceptionDAO exd = new ExceptionDAO(_ConStr); exd.SetExceptionLog(ApiKey, exobj, out ReturnVal, out ReturnMsg); resp.statuscode = (int)Common.ResponseStatusCode.Exception; resp.message = ex.Message.ToString(); response = BadRequest(resp); } return(response); }
private void SetUserApiToken(string ApiKey, UserApiTokenObj obj, out int ReturnVal, out string ReturnMsg) { AuthenticationDAO objAuthDAO = new AuthenticationDAO(_ConStr); objAuthDAO.SetUserApiToken(ApiKey, obj, out ReturnVal, out ReturnMsg); }