Commit 730545ef authored by alan.f's avatar alan.f

add-oidc-client

parent f52498b3
...@@ -10,7 +10,7 @@ using Microsoft.Extensions.Hosting; ...@@ -10,7 +10,7 @@ using Microsoft.Extensions.Hosting;
namespace ApiOne namespace ApiOne
{ {
public class Startup public class Startup
{ {
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
...@@ -23,7 +23,14 @@ namespace ApiOne ...@@ -23,7 +23,14 @@ namespace ApiOne
//config.RequireHttpsMetadata = false; //config.RequireHttpsMetadata = false;
}); });
services.AddCors(config =>
{
config.AddPolicy("AllowAll",
p => p.AllowAnyOrigin()
.AllowAnyMethod().
AllowAnyHeader()
);
});
services.AddHttpClient(); services.AddHttpClient();
//services.AddCors(confg => //services.AddCors(confg =>
// confg.AddPolicy("AllowAll", // confg.AddPolicy("AllowAll",
...@@ -41,7 +48,7 @@ namespace ApiOne ...@@ -41,7 +48,7 @@ namespace ApiOne
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
//app.UseCors("AllowAll"); app.UseCors("AllowAll");
app.UseRouting(); app.UseRouting();
......
...@@ -62,6 +62,7 @@ namespace IdentityServer ...@@ -62,6 +62,7 @@ namespace IdentityServer
ClientId="client_id_js", ClientId="client_id_js",
AllowedGrantTypes = GrantTypes.Implicit, AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris={ "https://localhost:44387/Home/signin" }, RedirectUris={ "https://localhost:44387/Home/signin" },
AllowedCorsOrigins={ "https://localhost:44387" },
AllowedScopes={ AllowedScopes={
IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.OpenId,
"ApiOne", "ApiOne",
......
...@@ -5,6 +5,20 @@ ...@@ -5,6 +5,20 @@
<h1>Home Index</h1> <h1>Home Index</h1>
<button onclick="signIn()">SignIn</button> <button onclick="signIn()">SignIn</button>
<script src="~/sign-in.js"></script> <div>
\ No newline at end of file <button onclick="signOut()">Sign Out</button>
</div>
<div>
<button onclick="callApi()">Call Api</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.9.1/oidc-client.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="~/main.js"></script>
\ No newline at end of file
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.9.1/oidc-client.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.9.1/oidc-client.min.js"></script>
<script>var userManger = new Oidc.UserManager({ <script>
userStore: new Oidc.WebStorageStateStore({ store: window.localStorage }), var userManger = new Oidc.UserManager();
response_mode: "query" //var userManger = new Oidc.UserManager({
}); // userStore: new Oidc.WebStorageStateStore({ store: window.localStorage }),
// response_mode: "query"
//});
userManger.signinCallback().then(res => { userManger.signinCallback().then(res => {
console.log(res); console.log(res);
......
...@@ -20,6 +20,5 @@ var signIn = function () { ...@@ -20,6 +20,5 @@ var signIn = function () {
"&state=" + createState(); "&state=" + createState();
var returnUrl = encodeURIComponent(authUrl); var returnUrl = encodeURIComponent(authUrl);
console.log(returnUrl);
window.location.href = "https://localhost:44325/Auth/Login?ReturnUrl=" + returnUrl; window.location.href = "https://localhost:44325/Auth/Login?ReturnUrl=" + returnUrl;
} }
\ No newline at end of file
var config = {
//userStore: new Oidc.WebStorageStateStore({ store: window.localStorage }),
authority: "https://localhost:44325/",
client_id: "client_id_js",
redirect_uri: "https://localhost:44387/Home/SignIn",
//post_logout_redirect_uri: "https://localhost:44345/Home/Index",
response_type: "id_token token",
scope: "openid ApiOne"
};
var userManager = new Oidc.UserManager(config);
var signIn = function () {
userManager.signinRedirect();
};
//var signOut = function () {
// userManager.signoutRedirect();
//};
userManager.getUser().then(user => {
console.log("user:", user);
if (user) {
axios.defaults.headers.common["Authorization"] = "Bearer " + user.access_token;
}
});
var callApi = function () {
axios.get("https://localhost:44340/secret")
.then(res => {
console.log(res);
});
};
//var refreshing = false;
//axios.interceptors.response.use(
// function (response) { return response; },
// function (error) {
// console.log("axios error:", error.response);
// var axiosConfig = error.response.config;
// //if error response is 401 try to refresh token
// if (error.response.status === 401) {
// console.log("axios error 401");
// // if already refreshing don't make another request
// if (!refreshing) {
// console.log("starting token refresh");
// refreshing = true;
// // do the refresh
// return userManager.signinSilent().then(user => {
// console.log("new user:", user);
// //update the http request and client
// axios.defaults.headers.common["Authorization"] = "Bearer " + user.access_token;
// axiosConfig.headers["Authorization"] = "Bearer " + user.access_token;
// //retry the http request
// return axios(axiosConfig);
// });
// }
// }
// return Promise.reject(error);
// });
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment