This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<T> GetAs<T>(string path) | |
{ | |
var response = await _client.GetStringAsync(path); | |
return JsonConvert.DeserializeObject<T>(response); | |
} | |
public async Task<string> Post(string path, object content) | |
{ | |
StringContent postContent = new StringContent(JsonConvert.SerializeObject(content)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
public class PersonController : Controller | |
{ | |
[HttpGet("person")] | |
public IActionResult GetPerson() | |
{ | |
return Ok(new Person() { Name = "John", Age = 23}); | |
} | |
[HttpPost("person")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
public class SandboxController : Controller | |
{ | |
private readonly RequestSender _requestSender; | |
public SandboxController() | |
{ | |
_requestSender = new RequestSender("https://localhost:44327/api/person/"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = (await _requestSender.PostAsync("postPerson", person, ContentType.XML)).ResultAs<Person>(ContentType.XML); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"alg": "HS256", | |
"typ": "JWT" | |
} | |
{ | |
"sub": "1234567890", | |
"name": "Adi Cucolaș", | |
"iat": 1516239022 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/api/login', (req, res) => { | |
// Mock user | |
const user = { | |
id: 1, | |
username: '1055984', | |
email: '1055984@ucn.com' | |
}; | |
jwt.sign({ user }, 'secretkey', | |
{ expiresIn: '30s' }, (err, token) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/api/posts', verifyToken, | |
(req, res) => { | |
jwt.verify(req.token, 'secretkey', | |
(err, authData) => { | |
if (err) { | |
res.sendStatus(403); | |
} else { | |
res.json({ | |
message: 'Post created...', | |
authData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FORMAT OF TOKEN | |
// Authorization: Bearer <access_token> | |
// Verify Token | |
function verifyToken(req, res, next) { | |
// Get auth header value | |
const bearerHeader = req.headers['authorization']; | |
// Check if bearer is undefined | |
if (typeof bearerHeader !== 'undefined') { | |
// Split at the space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/api/posts', | |
passport.authenticate('jwt', | |
{ session: false }), | |
handlePost) | |