Skip to content

Commit

Permalink
add buttons to show calling apis to MVC samples
Browse files Browse the repository at this point in the history
  • Loading branch information
brockallen committed Apr 20, 2016
1 parent 9691187 commit 5cbed36
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public ActionResult Claims()
{
ViewBag.Message = "Claims";

var cp = (ClaimsPrincipal)User;
ViewData["access_token"] = cp.FindFirst("access_token").Value;

return View();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
ViewBag.Title = "CallApi";
}

<h2>API result from MVC</h2>
<pre>@ViewBag.Json</pre>

31 changes: 30 additions & 1 deletion source/Clients/MVC OWIN Client (Hybrid)/Views/Home/Claims.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
<h3>@ViewBag.Message</h3>


<div>
<a class="btn btn-default" href="~/Home/CallApi">Call API from MVC</a>
<button id="api" class="btn btn-default">Call API from JS</button>
</div>

<div id="result" style="display:none">
<h2>API result from JS</h2>
<pre id="api_result"></pre>
</div>


<script>
var access_token = '@ViewData["access_token"]';
document.getElementById('api').addEventListener("click", function () {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://localhost:44379/identity");
xhr.onload = function () {
document.getElementById('result').style.display = "";
document.getElementById('api_result').innerText = JSON.stringify(JSON.parse(xhr.response), null, 2);
};
xhr.setRequestHeader("Authorization", "Bearer " + access_token);
xhr.send();
}, false);
</script>


<dl>
@foreach (var claim in System.Security.Claims.ClaimsPrincipal.Current.Claims)
{
Expand All @@ -10,4 +37,6 @@
@claim.Value
</dd>
}
</dl>
</dl>


22 changes: 20 additions & 2 deletions source/Clients/MVC OWIN Client/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Security.Claims;
using Newtonsoft.Json.Linq;
using Sample;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

Expand All @@ -21,12 +25,26 @@ public ActionResult Claims()

if (token != null)
{
ViewBag.Token = token.Value;
ViewData["access_token"] = token.Value;
}

return View();
}

public async Task<ActionResult> CallApi()
{
var token = (User as ClaimsPrincipal).FindFirst("access_token").Value;

var client = new HttpClient();
client.SetBearerToken(token);

var result = await client.GetStringAsync(Constants.AspNetWebApiSampleApi + "identity");
ViewBag.Json = JArray.Parse(result.ToString());

return View();
}


public ActionResult Signout()
{
Request.GetOwinContext().Authentication.SignOut();
Expand Down
25 changes: 23 additions & 2 deletions source/Clients/MVC OWIN Client/Views/Home/Claims.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
<h3>@ViewBag.Message</h3>

<div>
<a class="btn btn-default" href="~/Home/CallApi">Call API from MVC</a>
<button id="api" class="btn btn-default">Call API from JS</button>
</div>

<div id="result" style="display:none">
<h2>API result from JS</h2>
<pre id="api_result"></pre>
</div>


<script>
var token = '@ViewBag.Token';
var access_token = '@ViewData["access_token"]';
document.getElementById('api').addEventListener("click", function () {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://localhost:44379/identity");
xhr.onload = function () {
document.getElementById('result').style.display = "";
document.getElementById('api_result').innerText = JSON.stringify(JSON.parse(xhr.response), null, 2);
};
xhr.setRequestHeader("Authorization", "Bearer " + access_token);
xhr.send();
}, false);
</script>

<dl>
Expand All @@ -14,4 +35,4 @@
@claim.Value
</dd>
}
</dl>
</dl>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static HttpConfiguration Register()
// Web API routes
config.MapHttpAttributeRoutes();

config.EnableCors(new EnableCorsAttribute("http://localhost:21575, http://localhost:37045, http://localhost:37046", "accept, authorization", "GET", "WWW-Authenticate"));
config.EnableCors(new EnableCorsAttribute("https://localhost:44300, http://localhost:21575, http://localhost:37045, http://localhost:37046, https://localhost:44301", "accept, authorization", "GET", "WWW-Authenticate"));

config.Routes.MapHttpRoute(
name: "DefaultApi",
Expand Down
2 changes: 1 addition & 1 deletion source/Clients/SampleAspNetWebApi/Sample Web API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<AssemblyName>SampleAspNetWebApi</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressSSLPort>44379</IISExpressSSLPort>
<IISExpressAnonymousAuthentication>enabled</IISExpressAnonymousAuthentication>
<IISExpressWindowsAuthentication>disabled</IISExpressWindowsAuthentication>
<IISExpressUseClassicPipelineMode>false</IISExpressUseClassicPipelineMode>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="IdentityServer3, Version=2.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IdentityServer3.2.4.0\lib\net45\IdentityServer3.dll</HintPath>
<Reference Include="IdentityServer3, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IdentityServer3.2.5.0\lib\net45\IdentityServer3.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="IdentityServer3" version="2.4.0" targetFramework="net45" />
<package id="IdentityServer3" version="2.5.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" />
Expand Down

0 comments on commit 5cbed36

Please sign in to comment.