Hello every one Any way to hide network cal API in .net core mvc.
Loading
Hello every one Any way to hide network cal API in .net core mvc.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 2, 2025, 12:23 PM EST
Hello Saurabh,
You can achieve using below,
Instead of calling external APIs directly from the frontend, route all calls through your backend. This way:
[ApiController] [Route("api/[controller]")] public class ProxyController : ControllerBase { private readonly HttpClient _httpClient; public ProxyController(HttpClient httpClient) { _httpClient = httpClient; } [HttpGet("data")] public async Task GetData() { var externalApiUrl = "https://external.api/data"; var response = await _httpClient.GetAsync(externalApiUrl); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); return Ok(content); } return StatusCode((int)response.StatusCode); } }Jaish MathewsRank #225 · 11K reputationPosted Jan 2, 2025, 11:06 PM EST
Hiding API calls in .NET Core MVC can be achieved by ensuring that sensitive details, such as API keys or implementation logic, are not exposed to the client or public. Below are steps and best practices to achieve this:
1. Use a Backend Proxy
Instead of calling the API directly from the client-side JavaScript, route the API call through your .NET Core MVC backend.
HttpClientor similar backend service.Example:
[ApiController] [Route("api/[controller]")] public class ApiProxyController : ControllerBase { private readonly IHttpClientFactory _clientFactory; public ApiProxyController(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; } [HttpGet("data")] public async Task GetData() { var client = _clientFactory.CreateClient(); client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY"); var response = await client.GetAsync("https://external-api.com/data"); if (response.IsSuccessStatusCode) { var data = await response.Content.ReadAsStringAsync(); return Ok(data); // You can transform the data here if needed } return StatusCode((int)response.StatusCode, "Error fetching data"); } }2. Store API Keys Securely
Avoid hardcoding API keys in your source code. Instead, store them securely:
appsettings.json).Example (
appsettings.json):{ "ApiSettings": { "ApiKey": "your-api-key" } }Access in Code:
var apiKey = Configuration["ApiSettings:ApiKey"];3. Implement Authentication and Authorization
Ensure that only authorized users can access the API proxy endpoints:
[Authorize]attributes to restrict access.Example:
[Authorize] [HttpGet("secure-data")] public async Task GetSecureData() { // Logic for authenticated users }4. Limit Data Exposure
Only expose the data needed by the client. Filter or transform the data before returning it.
Example:
var filteredData = fullData.Select(x => new { x.Id, x.Name }); return Ok(filteredData);5. Hide API Endpoints in JavaScript
If your application requires API calls from JavaScript, avoid exposing sensitive API keys:
6. Rate Limiting and Throttling
Prevent abuse of your API proxy by implementing rate limiting or throttling:
7. Obfuscate API Call Patterns
These practices will help you effectively "hide" API calls while ensuring security and proper functionality in your .NET Core MVC application.
Sangeetha SRank #212 · 11.4K reputationPosted Jan 3, 2025, 12:04 AM EST
Hiding network call APIs in a .NET Core MVC application typically involves several strategies to protect sensitive information and improve security. Here are some approaches you can consider:
Environment Variables: Store sensitive information such as API keys and connection strings in environment variables instead of hard-coding them in your application. You can access these variables in your application using
IConfiguration.Configuration Files: Use
appsettings.jsonor other configuration files to store your API URLs and credentials. Make sure to mark sensitive information as "secret" when using user secrets in development.Backend Proxy: Instead of calling the API directly from the client-side (JavaScript), create a backend endpoint in your MVC application that acts as a proxy. The client makes a request to your backend, which then makes the API call. This way, the client never sees the actual API endpoint or credentials.
Authentication and Authorization: Implement authentication and authorization mechanisms to restrict access to your APIs. Use tokens (like JWT) for secure communication between your frontend and backend.
Rate Limiting and Monitoring: Consider implementing rate limiting on your API to prevent abuse and monitor for unusual activity.
Use HTTPS: Always use HTTPS for secure communication between your client and server, as well as between your server and any external APIs.
Obfuscation: While not a foolproof method, you could obfuscate parts of your code to make it harder for someone to understand the flow of API calls.