Browse Source

MVC auth updated to 2.0.0

pull/296/head
Eduard Tomas 7 years ago
parent
commit
fdd9a36719
5 changed files with 33 additions and 36 deletions
  1. +3
    -2
      src/Web/WebMVC/Controllers/AccountController.cs
  2. +1
    -1
      src/Web/WebMVC/Services/BasketService.cs
  3. +1
    -1
      src/Web/WebMVC/Services/CampaignService.cs
  4. +1
    -1
      src/Web/WebMVC/Services/OrderingService.cs
  5. +27
    -31
      src/Web/WebMVC/Startup.cs

+ 3
- 2
src/Web/WebMVC/Controllers/AccountController.cs View File

@ -22,7 +22,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
public async Task<IActionResult> SignIn(string returnUrl)
{
var user = User as ClaimsPrincipal;
var token = await HttpContext.Authentication.GetTokenAsync("access_token");
var token = await HttpContext.GetTokenAsync("access_token");
if (token != null)
{
@ -42,7 +43,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
// "Catalog" because UrlHelper doesn't support nameof() for controllers
// https://github.com/aspnet/Mvc/issues/5853
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 });
}
}
}

+ 1
- 1
src/Web/WebMVC/Services/BasketService.cs View File

@ -125,7 +125,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
async Task<string> GetUserTokenAsync()
{
var context = _httpContextAccesor.HttpContext;
return await context.Authentication.GetTokenAsync("access_token");
return await context.GetTokenAsync("access_token");
}
}
}

+ 1
- 1
src/Web/WebMVC/Services/CampaignService.cs View File

@ -64,7 +64,7 @@
private async Task<string> GetUserTokenAsync()
{
var context = _httpContextAccesor.HttpContext;
return await context.Authentication.GetTokenAsync("access_token");
return await context.GetTokenAsync("access_token");
}
}
}

+ 1
- 1
src/Web/WebMVC/Services/OrderingService.cs View File

@ -151,7 +151,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
{
var context = _httpContextAccesor.HttpContext;
return await context.Authentication.GetTokenAsync("access_token");
return await context.GetTokenAsync("access_token");
}
}
}

+ 27
- 31
src/Web/WebMVC/Startup.cs View File

@ -26,13 +26,6 @@ namespace Microsoft.eShopOnContainers.WebMVC
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Settings for the application
.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();
}
@ -84,6 +77,32 @@ namespace Microsoft.eShopOnContainers.WebMVC
{
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.
@ -106,32 +125,9 @@ namespace Microsoft.eShopOnContainers.WebMVC
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 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);
var log = loggerFactory.CreateLogger("identity");
app.UseMvc(routes =>
{


Loading…
Cancel
Save