Added User credential authentication
This commit is contained in:
parent
d86378a9a8
commit
a4a52d0383
@ -16,12 +16,15 @@ namespace EFB.Controllers.API
|
|||||||
|
|
||||||
this.HttpClient.DefaultRequestHeaders.Clear();
|
this.HttpClient.DefaultRequestHeaders.Clear();
|
||||||
|
|
||||||
|
if (Headers != null)
|
||||||
|
{
|
||||||
foreach (var Header in Headers)
|
foreach (var Header in Headers)
|
||||||
{
|
{
|
||||||
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
|
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
||||||
{
|
{
|
||||||
var pendingResult = this.HttpClient.GetAsync(Endpoint);
|
var pendingResult = this.HttpClient.GetAsync(Endpoint);
|
||||||
|
|
||||||
@ -41,22 +44,23 @@ namespace EFB.Controllers.API
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<T> Post<T>(string Endpoint, Dictionary<string, string> Headers, object Body){
|
public async Task<T> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
|
||||||
|
|
||||||
this.HttpClient = new HttpClient();
|
this.HttpClient = new HttpClient();
|
||||||
|
|
||||||
this.HttpClient.DefaultRequestHeaders.Clear();
|
//this.HttpClient.DefaultRequestHeaders.Clear();
|
||||||
|
|
||||||
|
if (Headers != null)
|
||||||
|
{
|
||||||
foreach (var Header in Headers)
|
foreach (var Header in Headers)
|
||||||
{
|
{
|
||||||
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
|
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json");
|
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
||||||
|
|
||||||
if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
|
||||||
{
|
{
|
||||||
var pendingResult = this.HttpClient.PostAsync(Endpoint, content);
|
var pendingResult = this.HttpClient.PostAsync(Endpoint, Body);
|
||||||
|
|
||||||
var result = await pendingResult;
|
var result = await pendingResult;
|
||||||
|
|
||||||
|
74
Controllers/UserController.cs
Normal file
74
Controllers/UserController.cs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Net.Http;
|
||||||
|
using EFB.Models.JSON;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace EFB.Controllers
|
||||||
|
{
|
||||||
|
//[Route("[controller]")]
|
||||||
|
public class UserController : Controller
|
||||||
|
{
|
||||||
|
private readonly ILogger<UserController> _logger;
|
||||||
|
|
||||||
|
public UserController(ILogger<UserController> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<IActionResult> Login(string email, string password){
|
||||||
|
|
||||||
|
if (Form.FormAuthenticator.ValidateEMail(email))
|
||||||
|
{
|
||||||
|
//API Helper
|
||||||
|
API.APIInterface API = new API.APIInterface();
|
||||||
|
|
||||||
|
//Dictionary of Formdata to be encoded
|
||||||
|
Dictionary<string, string> formData = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
formData.Add("grant_type", "client_credentials");
|
||||||
|
formData.Add("client_id", email);
|
||||||
|
formData.Add("client_secret", password);
|
||||||
|
|
||||||
|
HttpContent content = new FormUrlEncodedContent(formData);
|
||||||
|
|
||||||
|
var request = API.Post<Models.JSON.LoginResponse>("https://api.autorouter.aero/v1.0/oauth2/token", null, content);
|
||||||
|
|
||||||
|
//Wait for the response to come through
|
||||||
|
var response = await request;
|
||||||
|
|
||||||
|
if (response.error != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
TempData["Error"] = response.error_description;
|
||||||
|
return RedirectToAction("Index", "Home");
|
||||||
|
|
||||||
|
}else{
|
||||||
|
//Create a user session and continue
|
||||||
|
return RedirectToAction("Index", "Home");
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
TempData["Error"] = "Please enter a valid E-Mail";
|
||||||
|
return RedirectToAction("Index", "Home");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||||
|
public IActionResult Error()
|
||||||
|
{
|
||||||
|
return View("Error!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
Models/JSON/Login.cs
Normal file
19
Models/JSON/Login.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EFB.Models.JSON
|
||||||
|
{
|
||||||
|
public class Login
|
||||||
|
{
|
||||||
|
[JsonProperty]
|
||||||
|
public string grant_type { get; set; }
|
||||||
|
[JsonProperty]
|
||||||
|
public string client_id { get; set; }
|
||||||
|
[JsonProperty]
|
||||||
|
public string client_secret { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
28
Models/JSON/LoginResponse.cs
Normal file
28
Models/JSON/LoginResponse.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace EFB.Models.JSON
|
||||||
|
{
|
||||||
|
public class LoginResponse
|
||||||
|
{
|
||||||
|
[JsonProperty]
|
||||||
|
public string access_token { get; set; }
|
||||||
|
[JsonProperty]
|
||||||
|
public int expires_in { get; set; }
|
||||||
|
[JsonProperty]
|
||||||
|
public string token_type { get; set; }
|
||||||
|
[JsonProperty]
|
||||||
|
public string scope { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty]
|
||||||
|
public string error { get; set; } = null;
|
||||||
|
[JsonProperty]
|
||||||
|
public string error_description { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<form method="post" asp-controller="Home" asp-action="Login">
|
<form asp-controller="User" asp-action="Login">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<input type="text" class="form-control" placeholder="E-Mail" name="email" value="@TempData["email"]">
|
<input type="text" class="form-control" placeholder="E-Mail" name="email" value="@TempData["email"]">
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user