MVC auth updated to 2.0.0
This commit is contained in:
parent
bbc682d6a9
commit
fdd9a36719
@ -22,7 +22,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|||||||
public async Task<IActionResult> SignIn(string returnUrl)
|
public async Task<IActionResult> SignIn(string returnUrl)
|
||||||
{
|
{
|
||||||
var user = User as ClaimsPrincipal;
|
var user = User as ClaimsPrincipal;
|
||||||
var token = await HttpContext.Authentication.GetTokenAsync("access_token");
|
|
||||||
|
var token = await HttpContext.GetTokenAsync("access_token");
|
||||||
|
|
||||||
if (token != null)
|
if (token != null)
|
||||||
{
|
{
|
||||||
@ -42,7 +43,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|||||||
// "Catalog" because UrlHelper doesn't support nameof() for controllers
|
// "Catalog" because UrlHelper doesn't support nameof() for controllers
|
||||||
// https://github.com/aspnet/Mvc/issues/5853
|
// https://github.com/aspnet/Mvc/issues/5853
|
||||||
var homeUrl = Url.Action(nameof(CatalogController.Index), "Catalog");
|
var homeUrl = Url.Action(nameof(CatalogController.Index), "Catalog");
|
||||||
return new SignOutResult("oidc", new AuthenticationProperties { RedirectUri = homeUrl });
|
return new SignOutResult("oidc", new AspNetCore.Authentication.AuthenticationProperties { RedirectUri = homeUrl });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
|
|||||||
async Task<string> GetUserTokenAsync()
|
async Task<string> GetUserTokenAsync()
|
||||||
{
|
{
|
||||||
var context = _httpContextAccesor.HttpContext;
|
var context = _httpContextAccesor.HttpContext;
|
||||||
return await context.Authentication.GetTokenAsync("access_token");
|
return await context.GetTokenAsync("access_token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@
|
|||||||
private async Task<string> GetUserTokenAsync()
|
private async Task<string> GetUserTokenAsync()
|
||||||
{
|
{
|
||||||
var context = _httpContextAccesor.HttpContext;
|
var context = _httpContextAccesor.HttpContext;
|
||||||
return await context.Authentication.GetTokenAsync("access_token");
|
return await context.GetTokenAsync("access_token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -151,7 +151,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
|
|||||||
{
|
{
|
||||||
var context = _httpContextAccesor.HttpContext;
|
var context = _httpContextAccesor.HttpContext;
|
||||||
|
|
||||||
return await context.Authentication.GetTokenAsync("access_token");
|
return await context.GetTokenAsync("access_token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,6 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
|||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Settings for the application
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Settings for the application
|
||||||
.AddEnvironmentVariables(); // override settings with environment variables set in compose.
|
.AddEnvironmentVariables(); // override settings with environment variables set in compose.
|
||||||
|
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
|
||||||
{
|
|
||||||
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
|
|
||||||
builder.AddUserSecrets();
|
|
||||||
}
|
|
||||||
|
|
||||||
Configuration = builder.Build();
|
Configuration = builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +77,32 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
|||||||
{
|
{
|
||||||
services.AddSingleton<IHttpClient, StandardHttpClient>();
|
services.AddSingleton<IHttpClient, StandardHttpClient>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
||||||
|
var callBackUrl = Configuration.GetValue<string>("CallBackUrl");
|
||||||
|
// Add Authentication services
|
||||||
|
services.AddCookieAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||||
|
services.AddOpenIdConnectAuthentication("Oidc", options =>
|
||||||
|
{
|
||||||
|
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
|
options.Authority = identityUrl.ToString();
|
||||||
|
options.PostLogoutRedirectUri = callBackUrl.ToString();
|
||||||
|
options.ClientId = "mvc";
|
||||||
|
options.ClientSecret = "secret";
|
||||||
|
options.ResponseType = "code id_token";
|
||||||
|
options.SaveTokens = true;
|
||||||
|
options.GetClaimsFromUserInfoEndpoint = true;
|
||||||
|
options.RequireHttpsMetadata = false;
|
||||||
|
options.Scope.Add("openid");
|
||||||
|
options.Scope.Add("profile");
|
||||||
|
options.Scope.Add("orders");
|
||||||
|
options.Scope.Add("basket");
|
||||||
|
options.Scope.Add("marketing");
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddAuthentication(sharedOptions => sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
@ -106,33 +125,10 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
|||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
|
||||||
{
|
|
||||||
AuthenticationScheme = "Cookies",
|
|
||||||
AutomaticAuthenticate = true,
|
|
||||||
});
|
|
||||||
|
|
||||||
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
||||||
var callBackUrl = Configuration.GetValue<string>("CallBackUrl");
|
|
||||||
var log = loggerFactory.CreateLogger("identity");
|
var log = loggerFactory.CreateLogger("identity");
|
||||||
|
|
||||||
var oidcOptions = new OpenIdConnectOptions
|
|
||||||
{
|
|
||||||
SignInScheme = "Cookies",
|
|
||||||
Authority = identityUrl.ToString(),
|
|
||||||
PostLogoutRedirectUri = callBackUrl.ToString(),
|
|
||||||
ClientId = "mvc",
|
|
||||||
ClientSecret = "secret",
|
|
||||||
ResponseType = "code id_token",
|
|
||||||
SaveTokens = true,
|
|
||||||
GetClaimsFromUserInfoEndpoint = true,
|
|
||||||
RequireHttpsMetadata = false,
|
|
||||||
Scope = { "openid", "profile", "orders", "basket", "marketing" }
|
|
||||||
};
|
|
||||||
|
|
||||||
//Wait untill identity service is ready on compose.
|
|
||||||
app.UseOpenIdConnectAuthentication(oidcOptions);
|
|
||||||
|
|
||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
{
|
{
|
||||||
routes.MapRoute(
|
routes.MapRoute(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user