Skip to content

Commit

Permalink
Added AJAX call code
Browse files Browse the repository at this point in the history
  • Loading branch information
novogeek committed Oct 23, 2018
1 parent 35b9a0e commit e7aa764
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Darknet.Api/Startup.cs
Expand Up @@ -34,6 +34,7 @@ public void ConfigureServices(IServiceCollection services)
string signingKey = Configuration["ConnectionStrings:signingKey"];
byte[] signingKeyBytes = Encoding.ASCII.GetBytes(signingKey);

services.AddCors();
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHsts();
}

app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
Expand Down
1 change: 1 addition & 0 deletions Darknet.Web/Controllers/HomeController.cs
Expand Up @@ -69,6 +69,7 @@ public async Task<IActionResult> Index(string username)
};
ViewData["ImpersonationIDPUrl"] = _configOptions.IdpImpersonationUrl;
ViewData["ImpersonationRetUrl"] = $"returnUrl={_configOptions.WebBaseUrl}/Account/Implicit";
ViewData["ApiBaseUrl"] = _configOptions.ApiBaseUrl;
return View(userDetailsViewModel);
}
[HttpPost]
Expand Down
2 changes: 2 additions & 0 deletions Darknet.Web/Views/Home/Index.cshtml
Expand Up @@ -91,5 +91,7 @@
<div id="divHdn" class="hidden">
<input type="hidden" id="hdnImpersonationIDPUrl" value=@ViewData["ImpersonationIDPUrl"] />
<input type="hidden" id="hdnImpersonationRetUrl" value=@ViewData["ImpersonationRetUrl"] />
<input type="hidden" id="hdnApiBaseUrl" value=@ViewData["ApiBaseUrl"] />
<input type="hidden" id="hdnloggedInUser" value="@User.Identity.Name"/>
<iframe id="frmImplicit" src="" width="0" height="0"></iframe>
</div>
36 changes: 22 additions & 14 deletions Darknet.Web/wwwroot/js/site.js
Expand Up @@ -6,29 +6,37 @@
$(function () {
function getToken() {
var iframe = $('#frmImplicit');
var idpUrl = $('#hdnImpersonationIDPUrl').val();
var idpRetUrl = $('#hdnImpersonationRetUrl').val();
var apiBaseUrl = $('#hdnApiBaseUrl').val();
var loggedInUser = $('#hdnloggedInUser').val();

$("#ddFriends").change(function () {
var user = $('option:selected', this).val();
var idpUrl = $('#hdnImpersonationIDPUrl').val();
var idpRetUrl = $('#hdnImpersonationRetUrl').val();
var idpTokenUrl = idpUrl + '?username='+user+'&' + idpRetUrl;
var idpTokenUrl = idpUrl + '?username=' + user + '&' + idpRetUrl;
iframe.attr('src', idpTokenUrl);
});
iframe.on('load', () => {
var newHref = document.getElementById('frmImplicit').contentWindow.location.href;
let params = (new URL(newHref)).searchParams;
var token = params.get('token');
console.log('token:', token);
var apiUrl = apiBaseUrl + '/api/UserDetailsApi/GetPostsOfTargetUser?targetUser=' + loggedInUser;

//$.ajax({
// url: 'https://api.sandbox.slcedu.org/api/rest/v1/students/test1',
// type: 'GET',
// beforeSend: function (xhr) {
// xhr.setRequestHeader('Authorization', 'Bearer '+token);
// },
// data: {},
// success: function () { },
// error: function () { },
//});
$.ajax({
url: apiUrl,
crossDomain: true,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
data: {},
success: function (resp) {
console.log('success: ', resp);
},
error: function (err) {
console.log('error: ', err);
}
});
});
}
getToken();
Expand Down

0 comments on commit e7aa764

Please sign in to comment.