diff --git a/AspNetCore.sln b/AspNetCore.sln index 4f798d3ca2d3..d5fb1ad69361 100644 --- a/AspNetCore.sln +++ b/AspNetCore.sln @@ -1130,8 +1130,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JwtBearerSample", "src\Secu EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E19E55A2-1562-47A7-8EA6-B51F2CA0CC4C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenIdConnect.AzureAdSample", "src\Security\Authentication\OpenIdConnect\samples\OpenIdConnect.AzureAdSample\OpenIdConnect.AzureAdSample.csproj", "{E3867FCF-70D3-40B0-BCF6-208D4D971666}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenIdConnectSample", "src\Security\Authentication\OpenIdConnect\samples\OpenIdConnectSample\OpenIdConnectSample.csproj", "{F6B80862-7D74-4362-B5E2-AEBF116F2B19}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WsFederation", "WsFederation", "{602CE06C-E973-401D-8877-90957E613646}" @@ -5672,18 +5670,6 @@ Global {88C2C45B-ED16-4B85-8498-E3F13679629D}.Release|x64.Build.0 = Release|Any CPU {88C2C45B-ED16-4B85-8498-E3F13679629D}.Release|x86.ActiveCfg = Release|Any CPU {88C2C45B-ED16-4B85-8498-E3F13679629D}.Release|x86.Build.0 = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|x64.ActiveCfg = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|x64.Build.0 = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|x86.ActiveCfg = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Debug|x86.Build.0 = Debug|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|Any CPU.Build.0 = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|x64.ActiveCfg = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|x64.Build.0 = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|x86.ActiveCfg = Release|Any CPU - {E3867FCF-70D3-40B0-BCF6-208D4D971666}.Release|x86.Build.0 = Release|Any CPU {F6B80862-7D74-4362-B5E2-AEBF116F2B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F6B80862-7D74-4362-B5E2-AEBF116F2B19}.Debug|Any CPU.Build.0 = Debug|Any CPU {F6B80862-7D74-4362-B5E2-AEBF116F2B19}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -8268,7 +8254,6 @@ Global {7FD32066-C831-4E29-978C-9A2215E85C67} = {2AB4FE8F-94FF-4C6E-B12F-9E40A9FAF9AB} {88C2C45B-ED16-4B85-8498-E3F13679629D} = {7FD32066-C831-4E29-978C-9A2215E85C67} {E19E55A2-1562-47A7-8EA6-B51F2CA0CC4C} = {99F9BBC5-46AE-40DC-A675-5E56ACA3C5C8} - {E3867FCF-70D3-40B0-BCF6-208D4D971666} = {E19E55A2-1562-47A7-8EA6-B51F2CA0CC4C} {F6B80862-7D74-4362-B5E2-AEBF116F2B19} = {E19E55A2-1562-47A7-8EA6-B51F2CA0CC4C} {602CE06C-E973-401D-8877-90957E613646} = {822D1519-77F0-484A-B9AB-F694C2CC25F1} {BECDA951-C285-485D-BFE6-F770BC8C9EB2} = {602CE06C-E973-401D-8877-90957E613646} diff --git a/eng/Dependencies.props b/eng/Dependencies.props index a52dd7acd6f0..bcea17e840d8 100644 --- a/eng/Dependencies.props +++ b/eng/Dependencies.props @@ -115,7 +115,6 @@ and are generated based on the last package release. - diff --git a/eng/Versions.props b/eng/Versions.props index 307cc9c290a5..ce88d36a054a 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -195,7 +195,6 @@ 3.8.0 3.3.0 1.0.0-20200708.1 - 3.19.8 6.10.0 6.10.0 6.10.0 diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/AuthPropertiesTokenCache.cs b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/AuthPropertiesTokenCache.cs deleted file mode 100644 index 7d9b391213ba..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/AuthPropertiesTokenCache.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Security.Claims; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Http; -using Microsoft.IdentityModel.Clients.ActiveDirectory; - -namespace OpenIdConnect.AzureAdSample -{ - public class AuthPropertiesTokenCache : TokenCache - { - private const string TokenCacheKey = ".TokenCache"; - - private HttpContext _httpContext; - private ClaimsPrincipal _principal; - private AuthenticationProperties _authProperties; - private string _signInScheme; - - private AuthPropertiesTokenCache(AuthenticationProperties authProperties) : base() - { - _authProperties = authProperties; - BeforeAccess = BeforeAccessNotificationWithProperties; - AfterAccess = AfterAccessNotificationWithProperties; - BeforeWrite = BeforeWriteNotification; - } - - private AuthPropertiesTokenCache(HttpContext httpContext, string signInScheme) : base() - { - _httpContext = httpContext; - _signInScheme = signInScheme; - BeforeAccess = BeforeAccessNotificationWithContext; - AfterAccess = AfterAccessNotificationWithContext; - BeforeWrite = BeforeWriteNotification; - } - - public static TokenCache ForCodeRedemption(AuthenticationProperties authProperties) - { - return new AuthPropertiesTokenCache(authProperties); - } - - public static TokenCache ForApiCalls(HttpContext httpContext, - string signInScheme = CookieAuthenticationDefaults.AuthenticationScheme) - { - return new AuthPropertiesTokenCache(httpContext, signInScheme); - } - - private void BeforeAccessNotificationWithProperties(TokenCacheNotificationArgs args) - { - string cachedTokensText; - if (_authProperties.Items.TryGetValue(TokenCacheKey, out cachedTokensText)) - { - var cachedTokens = Convert.FromBase64String(cachedTokensText); - Deserialize(cachedTokens); - } - } - - private void BeforeAccessNotificationWithContext(TokenCacheNotificationArgs args) - { - // Retrieve the auth session with the cached tokens - var result = _httpContext.AuthenticateAsync(_signInScheme).Result; - _authProperties = result.Ticket.Properties; - _principal = result.Ticket.Principal; - - BeforeAccessNotificationWithProperties(args); - } - - private void AfterAccessNotificationWithProperties(TokenCacheNotificationArgs args) - { - // if state changed - if (HasStateChanged) - { - var cachedTokens = Serialize(); - var cachedTokensText = Convert.ToBase64String(cachedTokens); - _authProperties.Items[TokenCacheKey] = cachedTokensText; - } - } - - private void AfterAccessNotificationWithContext(TokenCacheNotificationArgs args) - { - // if state changed - if (HasStateChanged) - { - AfterAccessNotificationWithProperties(args); - - var cachedTokens = Serialize(); - var cachedTokensText = Convert.ToBase64String(cachedTokens); - _authProperties.Items[TokenCacheKey] = cachedTokensText; - _httpContext.SignInAsync(_signInScheme, _principal, _authProperties).Wait(); - } - } - - private void BeforeWriteNotification(TokenCacheNotificationArgs args) - { - // if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry - } - } -} diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/OpenIdConnect.AzureAdSample.csproj b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/OpenIdConnect.AzureAdSample.csproj deleted file mode 100644 index 28c163ceee07..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/OpenIdConnect.AzureAdSample.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - $(DefaultNetCoreTargetFramework) - aspnet5-OpenIdConnectSample-20151210110318 - OutOfProcess - - - - - - - - - - diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Program.cs b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Program.cs deleted file mode 100644 index 1ec249d54e5d..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Program.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; - -namespace OpenIdConnect.AzureAdSample -{ - public static class Program - { - public static Task Main(string[] args) - { - var host = Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webHostBuilder => - { - webHostBuilder - .UseStartup(); - }) - .Build(); - - return host.RunAsync(); - } - } -} diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Properties/launchSettings.json b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Properties/launchSettings.json deleted file mode 100644 index 8e1557bb7f09..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "https://localhost:44318/", - "sslPort": 44318 - } - }, - "profiles": { - "SocialSample": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "https://localhost:44318/" - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Readme.md b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Readme.md deleted file mode 100644 index 767e336ac609..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# How to set up the sample locally - -## Set up [Azure Active Directory](https://azure.microsoft.com/en-us/documentation/services/active-directory/) - -1. Create your own Azure Active Directory (AD). Save the "tenent name". -2. Add a new Application: in the Azure AD portal, select Application, and click Add in the drawer. -3. Set the sign-on url to `http://localhost:42023`. -4. Select the newly created Application, navigate to the Configure tab. -5. Find and save the "Client Id" -8. In the keys section add a new key. A key value will be generated. Save the value as "Client Secret" - -## Configure the local environment -1. Set environment ASPNETCORE_ENVIRONMENT to DEVELOPMENT. ([Working with Multiple Environments](https://docs.asp.net/en/latest/fundamentals/environments.html)) -2. Set up user secrets: -``` -dotnet user-secrets set oidc:clientid -dotnet user-secrets set oidc:clientsecret -dotnet user-secrets set oidc:authority https://login.windows.net/.onmicrosoft.com -``` - diff --git a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Startup.cs b/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Startup.cs deleted file mode 100644 index 78e9863f23ae..000000000000 --- a/src/Security/Authentication/OpenIdConnect/samples/OpenIdConnect.AzureAdSample/Startup.cs +++ /dev/null @@ -1,193 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.Encodings.Web; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Extensions; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.IdentityModel.Clients.ActiveDirectory; -using Microsoft.IdentityModel.Protocols.OpenIdConnect; - -namespace OpenIdConnect.AzureAdSample -{ - public class Startup - { - public Startup(IConfiguration config) - { - Configuration = config; - } - - public IConfiguration Configuration { get; set; } - - private string ClientId => Configuration["oidc:clientid"]; - private string ClientSecret => Configuration["oidc:clientsecret"]; - private string Authority => Configuration["oidc:authority"]; - private string Resource => "https://graph.windows.net"; - - public void ConfigureServices(IServiceCollection services) - { - services.AddAuthentication(sharedOptions => - { - sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; - sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; - }) - .AddCookie() - .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, "AAD", o => - { - o.ClientId = ClientId; - o.ClientSecret = ClientSecret; // for code flow - o.Authority = Authority; - o.ResponseType = OpenIdConnectResponseType.CodeIdToken; - o.SignedOutRedirectUri = "/signed-out"; - // GetClaimsFromUserInfoEndpoint = true, - o.Events = new OpenIdConnectEvents() - { - OnAuthorizationCodeReceived = async context => - { - var request = context.HttpContext.Request; - var currentUri = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path); - var credential = new ClientCredential(ClientId, ClientSecret); - var authContext = new AuthenticationContext(Authority, AuthPropertiesTokenCache.ForCodeRedemption(context.Properties)); - - var result = await authContext.AcquireTokenByAuthorizationCodeAsync( - context.ProtocolMessage.Code, new Uri(currentUri), credential, Resource); - - context.HandleCodeRedemption(result.AccessToken, result.IdToken); - } - }; - }); - } - - public void Configure(IApplicationBuilder app) - { - app.UseDeveloperExceptionPage(); - - app.UseAuthentication(); - - app.Run(async context => - { - if (context.Request.Path.Equals("/signin")) - { - if (context.User.Identities.Any(identity => identity.IsAuthenticated)) - { - // User has already signed in - context.Response.Redirect("/"); - return; - } - - await context.ChallengeAsync(new AuthenticationProperties { RedirectUri = "/" }); - } - else if (context.Request.Path.Equals("/signout")) - { - await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - await WriteHtmlAsync(context.Response, - async response => - { - await response.WriteAsync($"

Signed out locally: {HtmlEncode(context.User.Identity.Name)}

"); - await response.WriteAsync("Sign In"); - }); - } - else if (context.Request.Path.Equals("/signout-remote")) - { - await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); - } - else if (context.Request.Path.Equals("/signed-out")) - { - await WriteHtmlAsync(context.Response, - async response => - { - await response.WriteAsync($"

You have been signed out.

"); - await response.WriteAsync("Sign In"); - }); - } - else if (context.Request.Path.Equals("/remote-signedout")) - { - await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - await WriteHtmlAsync(context.Response, - async response => - { - await response.WriteAsync($"

Signed out remotely: {HtmlEncode(context.User.Identity.Name)}

"); - await response.WriteAsync("Sign In"); - }); - } - else - { - if (!context.User.Identities.Any(identity => identity.IsAuthenticated)) - { - await context.ChallengeAsync(new AuthenticationProperties { RedirectUri = "/" }); - return; - } - - await WriteHtmlAsync(context.Response, async response => - { - await response.WriteAsync($"

Hello Authenticated User {HtmlEncode(context.User.Identity.Name)}

"); - await response.WriteAsync("Sign Out Locally"); - await response.WriteAsync("Sign Out Remotely"); - - await response.WriteAsync("

Claims:

"); - await WriteTableHeader(response, new string[] { "Claim Type", "Value" }, context.User.Claims.Select(c => new string[] { c.Type, c.Value })); - - await response.WriteAsync("

Tokens:

"); - try - { - // Use ADAL to get the right token - var authContext = new AuthenticationContext(Authority, AuthPropertiesTokenCache.ForApiCalls(context, CookieAuthenticationDefaults.AuthenticationScheme)); - var credential = new ClientCredential(ClientId, ClientSecret); - string userObjectID = context.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; - var result = await authContext.AcquireTokenSilentAsync(Resource, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); - - await response.WriteAsync($"

access_token

{HtmlEncode(result.AccessToken)}
"); - } - catch (Exception ex) - { - await response.WriteAsync($"AcquireToken error: {ex.Message}"); - } - }); - } - }); - } - - private static async Task WriteHtmlAsync(HttpResponse response, Func writeContent) - { - var bootstrap = ""; - - response.ContentType = "text/html"; - await response.WriteAsync($"{bootstrap}
"); - await writeContent(response); - await response.WriteAsync("
"); - } - - private static async Task WriteTableHeader(HttpResponse response, IEnumerable columns, IEnumerable> data) - { - await response.WriteAsync(""); - await response.WriteAsync(""); - foreach (var column in columns) - { - await response.WriteAsync($""); - } - await response.WriteAsync(""); - foreach (var row in data) - { - await response.WriteAsync(""); - foreach (var column in row) - { - await response.WriteAsync($""); - } - await response.WriteAsync(""); - } - await response.WriteAsync("
{HtmlEncode(column)}
{HtmlEncode(column)}
"); - } - - private static string HtmlEncode(string content) => - string.IsNullOrEmpty(content) ? string.Empty : HtmlEncoder.Default.Encode(content); - } -} -