diff --git a/.gitignore b/.gitignore index 9bf552b0..93bf8611 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ target/ .nuget/ .vs/ +.build/ # User-specific files *.suo diff --git a/.travis.yml b/.travis.yml index 0e60c2d3..bf811dc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,20 @@ language: csharp -sudo: false -script: - - ./build.sh verify +sudo: required +dist: trusty +addons: + apt: + packages: + - gettext + - libcurl4-openssl-dev + - libicu-dev + - libssl-dev + - libunwind8 + - zlib1g mono: - - alpha - - latest \ No newline at end of file + - 4.0.5 +os: + - linux + - osx +osx_image: xcode7.1 +script: + - ./build.sh verify \ No newline at end of file diff --git a/NuGet.config b/NuGet.config index ab552516..91bc2845 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,10 +1,8 @@ - + - - - - + + - + \ No newline at end of file diff --git a/build.cmd b/build.cmd index 31538727..d693f6c8 100644 --- a/build.cmd +++ b/build.cmd @@ -1,40 +1,40 @@ -@echo off -cd %~dp0 - +@ECHO off SETLOCAL + +SET REPO_FOLDER=%~dp0 +CD "%REPO_FOLDER%" + +SET BUILD_FOLDER=.build +SET KOREBUILD_FOLDER=%BUILD_FOLDER%\KoreBuild-dotnet +SET KOREBUILD_VERSION= + +SET NUGET_PATH=%BUILD_FOLDER%\NuGet.exe SET NUGET_VERSION=latest -SET CACHED_NUGET="%LocalAppData%\NuGet\nuget.%NUGET_VERSION%.exe" -SET BUILDCMD_KOREBUILD_VERSION= -SET BUILDCMD_DNX_VERSION= - -IF EXIST %CACHED_NUGET% goto copynuget -echo Downloading latest version of NuGet.exe... -IF NOT EXIST "%LocalAppData%\NuGet" md "%LocalAppData%\NuGet" -@powershell -NoProfile -ExecutionPolicy unrestricted -Command "$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest 'https://dist.nuget.org/win-x86-commandline/%NUGET_VERSION%/nuget.exe' -OutFile '%CACHED_NUGET%'" - -:copynuget -IF EXIST .nuget\nuget.exe goto restore -md .nuget -copy %CACHED_NUGET% .nuget\nuget.exe > nul - -:restore -IF EXIST packages\Sake goto getdnx -IF "%BUILDCMD_KOREBUILD_VERSION%"=="" ( - .nuget\nuget.exe install KoreBuild -ExcludeVersion -o packages -nocache -pre -) ELSE ( - .nuget\nuget.exe install KoreBuild -version %BUILDCMD_KOREBUILD_VERSION% -ExcludeVersion -o packages -nocache -pre +SET CACHED_NUGET=%LocalAppData%\NuGet\nuget.%NUGET_VERSION%.exe + +IF NOT EXIST "%BUILD_FOLDER%" ( + md "%BUILD_FOLDER%" ) -.nuget\NuGet.exe install Sake -ExcludeVersion -Source https://www.nuget.org/api/v2/ -Out packages -:getdnx -IF "%BUILDCMD_DNX_VERSION%"=="" ( - SET BUILDCMD_DNX_VERSION=latest +IF NOT EXIST "%NUGET_PATH%" ( + IF NOT EXIST "%CACHED_NUGET%" ( + echo Downloading latest version of NuGet.exe... + IF NOT EXIST "%LocalAppData%\NuGet" ( + md "%LocalAppData%\NuGet" + ) + @powershell -NoProfile -ExecutionPolicy unrestricted -Command "$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest 'https://dist.nuget.org/win-x86-commandline/%NUGET_VERSION%/nuget.exe' -OutFile '%CACHED_NUGET%'" + ) + + copy "%CACHED_NUGET%" "%NUGET_PATH%" > nul ) -IF "%SKIP_DNX_INSTALL%"=="" ( - CALL packages\KoreBuild\build\dnvm install %BUILDCMD_DNX_VERSION% -runtime CoreCLR -arch x86 -alias default - CALL packages\KoreBuild\build\dnvm install default -runtime CLR -arch x86 -alias default -) ELSE ( - CALL packages\KoreBuild\build\dnvm use default -runtime CLR -arch x86 + +IF NOT EXIST "%KOREBUILD_FOLDER%" ( + SET KOREBUILD_DOWNLOAD_ARGS= + IF NOT "%KOREBUILD_VERSION%"=="" ( + SET KOREBUILD_DOWNLOAD_ARGS=-version %KOREBUILD_VERSION% + ) + + "%BUILD_FOLDER%\nuget.exe" install KoreBuild-dotnet -ExcludeVersion -o "%BUILD_FOLDER%" -nocache -pre %KOREBUILD_DOWNLOAD_ARGS% ) -packages\Sake\tools\Sake.exe -I packages\KoreBuild\build -f makefile.shade %* \ No newline at end of file +"%KOREBUILD_FOLDER%\build\KoreBuild.cmd" %* diff --git a/build.sh b/build.sh index 9152db16..263fb667 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,10 @@ #!/usr/bin/env bash +buildFolder=.build +koreBuildFolder=$buildFolder/KoreBuild-dotnet + +nugetPath=$buildFolder/nuget.exe + if test `uname` = Darwin; then cachedir=~/Library/Caches/KBuild else @@ -11,33 +16,30 @@ else fi mkdir -p $cachedir nugetVersion=latest -cachePath=$cachedir/nuget.$nugetVersion.exe +cacheNuget=$cachedir/nuget.$nugetVersion.exe -url=https://dist.nuget.org/win-x86-commandline/$nugetVersion/nuget.exe +nugetUrl=https://dist.nuget.org/win-x86-commandline/$nugetVersion/nuget.exe -if test ! -f $cachePath; then - wget -O $cachePath $url 2>/dev/null || curl -o $cachePath --location $url /dev/null +if test ! -d $buildFolder; then + mkdir $buildFolder fi -if test ! -e .nuget; then - mkdir .nuget - cp $cachePath .nuget/nuget.exe -fi +if test ! -f $nugetPath; then + if test ! -f $cacheNuget; then + wget -O $cacheNuget $nugetUrl 2>/dev/null || curl -o $cacheNuget --location $nugetUrl /dev/null + fi -if test ! -d packages/Sake; then - mono .nuget/nuget.exe install KoreBuild -ExcludeVersion -o packages -nocache -pre - mono .nuget/nuget.exe install Sake -ExcludeVersion -Source https://www.nuget.org/api/v2/ -Out packages + cp $cacheNuget $nugetPath fi -if ! type dnvm > /dev/null 2>&1; then - source packages/KoreBuild/build/dnvm.sh +if test ! -d $koreBuildFolder; then + mono $nugetPath install KoreBuild-dotnet -ExcludeVersion -o $buildFolder -nocache -pre + chmod +x $koreBuildFolder/build/KoreBuild.sh fi -if ! type dnx > /dev/null 2>&1 || [ -z "$SKIP_DNX_INSTALL" ]; then - dnvm install latest -runtime coreclr -alias default - dnvm install default -runtime mono -alias default -else - dnvm use default -runtime mono +makeFile=makefile.shade +if [ ! -e $makeFile ]; then + makeFile=$koreBuildFolder/build/makefile.shade fi -mono packages/Sake/tools/Sake.exe -I packages/KoreBuild/build -f makefile.shade "$@" \ No newline at end of file +./$koreBuildFolder/build/KoreBuild.sh -n $nugetPath -m $makeFile "$@" diff --git a/external/NWebsec/ApplicationBuilderExtensions.cs b/external/NWebsec/ApplicationBuilderExtensions.cs index c32a8e73..d64f25b0 100644 --- a/external/NWebsec/ApplicationBuilderExtensions.cs +++ b/external/NWebsec/ApplicationBuilderExtensions.cs @@ -6,7 +6,7 @@ using NWebsec.Middleware; using NWebsec.Middleware.Middleware; // ReSharper disable once CheckNamespace -namespace Microsoft.AspNet.Builder +namespace Microsoft.AspNetCore.Builder { public static class ApplicationBuilderExtensions { diff --git a/external/NWebsec/Extensions/HttpContextExtensions.cs b/external/NWebsec/Extensions/HttpContextExtensions.cs index 0032329c..b488c5bd 100644 --- a/external/NWebsec/Extensions/HttpContextExtensions.cs +++ b/external/NWebsec/Extensions/HttpContextExtensions.cs @@ -1,6 +1,6 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Http; namespace NWebsec.Core.Extensions { diff --git a/external/NWebsec/Helpers/CspUpgradeHelper.cs b/external/NWebsec/Helpers/CspUpgradeHelper.cs index 2e93d2e3..ee2964e8 100644 --- a/external/NWebsec/Helpers/CspUpgradeHelper.cs +++ b/external/NWebsec/Helpers/CspUpgradeHelper.cs @@ -4,7 +4,7 @@ using System; using System.Linq; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Http; namespace NWebsec.Middleware.Helpers { diff --git a/external/NWebsec/Helpers/HeaderResultHandler.cs b/external/NWebsec/Helpers/HeaderResultHandler.cs index f2ce79f9..6708b5ac 100644 --- a/external/NWebsec/Helpers/HeaderResultHandler.cs +++ b/external/NWebsec/Helpers/HeaderResultHandler.cs @@ -1,6 +1,6 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Http; using NWebsec.Core.HttpHeaders; namespace NWebsec.Core.Helpers diff --git a/external/NWebsec/Helpers/IHeaderResultHandler.cs b/external/NWebsec/Helpers/IHeaderResultHandler.cs index 190f419c..fc665cb1 100644 --- a/external/NWebsec/Helpers/IHeaderResultHandler.cs +++ b/external/NWebsec/Helpers/IHeaderResultHandler.cs @@ -1,6 +1,6 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Http; using NWebsec.Core.HttpHeaders; namespace NWebsec.Core.Helpers diff --git a/external/NWebsec/Middleware/CspMiddleware.cs b/external/NWebsec/Middleware/CspMiddleware.cs index 6ace7746..6f1c26f6 100644 --- a/external/NWebsec/Middleware/CspMiddleware.cs +++ b/external/NWebsec/Middleware/CspMiddleware.cs @@ -2,8 +2,8 @@ using System; using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/HpkpMiddleware.cs b/external/NWebsec/Middleware/HpkpMiddleware.cs index 8a48a6ad..2a744860 100644 --- a/external/NWebsec/Middleware/HpkpMiddleware.cs +++ b/external/NWebsec/Middleware/HpkpMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/HstsMiddleware.cs b/external/NWebsec/Middleware/HstsMiddleware.cs index 03e7d6ea..84cd404f 100644 --- a/external/NWebsec/Middleware/HstsMiddleware.cs +++ b/external/NWebsec/Middleware/HstsMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; using NWebsec.Middleware.Helpers; diff --git a/external/NWebsec/Middleware/MiddleWareBase.cs b/external/NWebsec/Middleware/MiddleWareBase.cs index e8529d5a..7227d537 100644 --- a/external/NWebsec/Middleware/MiddleWareBase.cs +++ b/external/NWebsec/Middleware/MiddleWareBase.cs @@ -1,8 +1,8 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; namespace NWebsec.Middleware.Middleware { diff --git a/external/NWebsec/Middleware/RedirectValidationMiddleware.cs b/external/NWebsec/Middleware/RedirectValidationMiddleware.cs index 0cfa5571..eff6c6fa 100644 --- a/external/NWebsec/Middleware/RedirectValidationMiddleware.cs +++ b/external/NWebsec/Middleware/RedirectValidationMiddleware.cs @@ -1,8 +1,8 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. using System; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core; namespace NWebsec.Middleware.Middleware diff --git a/external/NWebsec/Middleware/XContentTypeOptionsMiddleware.cs b/external/NWebsec/Middleware/XContentTypeOptionsMiddleware.cs index 7e9f31a3..be22465d 100644 --- a/external/NWebsec/Middleware/XContentTypeOptionsMiddleware.cs +++ b/external/NWebsec/Middleware/XContentTypeOptionsMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/XDownloadOptionsMiddleware.cs b/external/NWebsec/Middleware/XDownloadOptionsMiddleware.cs index ad62ea49..2347028a 100644 --- a/external/NWebsec/Middleware/XDownloadOptionsMiddleware.cs +++ b/external/NWebsec/Middleware/XDownloadOptionsMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/XRobotsTagMiddleware.cs b/external/NWebsec/Middleware/XRobotsTagMiddleware.cs index a4322b03..3c1a29e1 100644 --- a/external/NWebsec/Middleware/XRobotsTagMiddleware.cs +++ b/external/NWebsec/Middleware/XRobotsTagMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/XXssMiddleware.cs b/external/NWebsec/Middleware/XXssMiddleware.cs index 819382fd..ed285016 100644 --- a/external/NWebsec/Middleware/XXssMiddleware.cs +++ b/external/NWebsec/Middleware/XXssMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/Middleware/XfoMiddleware.cs b/external/NWebsec/Middleware/XfoMiddleware.cs index b12af824..d91648b7 100644 --- a/external/NWebsec/Middleware/XfoMiddleware.cs +++ b/external/NWebsec/Middleware/XfoMiddleware.cs @@ -1,7 +1,7 @@ // Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using NWebsec.Core.Extensions; using NWebsec.Core.HttpHeaders; using NWebsec.Core.HttpHeaders.Configuration; diff --git a/external/NWebsec/project.json b/external/NWebsec/project.json index 11e63a01..3a86420b 100644 --- a/external/NWebsec/project.json +++ b/external/NWebsec/project.json @@ -1,30 +1,30 @@ { - "version": "1.0.0-internal-*", - "title": "NWebsec.Middleware (ASP.NET 5)", - "description": "NWebsec middleware for ASP.NET 5 applications. NWebsec helps you set important security headers and detect potentially dangerous redirects. See project website for documentation.", - "releaseNotes": "This release includes core functions for the NWebsec security libaries.", - "authors": [ "André N. Klingsheim" ], - "owners": [ "André N. Klingsheim" ], - "copyright": "Copyright © 2014 - 2015", - "tags": [ "NWebsec Security AspNet AppSec" ], - "projectUrl": "https://docs.nwebsec.com/en/latest/", - "licenseUrl": "https://github.com/NWebsec/NWebsec/blob/master/LICENSE", - "iconUrl": "https://klings.blob.core.windows.net/nwebsecicon/nwebsec_nuget.png", + "version": "1.0.0-internal-*", + "title": "NWebsec.Middleware (ASP.NET 5)", + "description": "NWebsec middleware for ASP.NET 5 applications. NWebsec helps you set important security headers and detect potentially dangerous redirects. See project website for documentation.", + "releaseNotes": "This release includes core functions for the NWebsec security libaries.", + "authors": [ "André N. Klingsheim" ], + "owners": [ "André N. Klingsheim" ], + "copyright": "Copyright © 2014 - 2015", + "tags": [ "NWebsec Security AspNet AppSec" ], + "projectUrl": "https://docs.nwebsec.com/en/latest/", + "licenseUrl": "https://github.com/NWebsec/NWebsec/blob/master/LICENSE", + "iconUrl": "https://klings.blob.core.windows.net/nwebsecicon/nwebsec_nuget.png", - "shared": "**/**.cs", + "shared": "**/**.cs", - "frameworks": { - "dnx451": { }, - "net451": { }, - "dotnet5.4": { - "dependencies": { - "Microsoft.CSharp": "4.0.1-*", - "System.Runtime": "4.0.21-*" - } - } + "frameworks": { + "net451": { }, + "dotnet5.4": { + "dependencies": { + "Microsoft.CSharp": "4.0.1-*", + "System.Runtime": "4.1.0-*" + } + } + + }, - }, - "dependencies": { - "Microsoft.AspNet.Http.Abstractions": "1.0.0-rc2-*" - } + "dependencies": { + "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-*" + } } diff --git a/global.json b/global.json index 942c484a..90b78172 100644 --- a/global.json +++ b/global.json @@ -1 +1,3 @@ -{ "sources": [ "src", "external" ] } \ No newline at end of file +{ + "projects": [ "src", "external" ] +} \ No newline at end of file diff --git a/samples/Mvc.Client/Controllers/AuthenticationController.cs b/samples/Mvc.Client/Controllers/AuthenticationController.cs index 82686970..8b71306d 100644 --- a/samples/Mvc.Client/Controllers/AuthenticationController.cs +++ b/samples/Mvc.Client/Controllers/AuthenticationController.cs @@ -1,8 +1,8 @@ using System.Threading.Tasks; -using Microsoft.AspNet.Authentication.Cookies; -using Microsoft.AspNet.Authentication.OpenIdConnect; -using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Mvc; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.AspNetCore.Http.Authentication; +using Microsoft.AspNetCore.Mvc; namespace Mvc.Client.Controllers { public class AuthenticationController : Controller { diff --git a/samples/Mvc.Client/Controllers/HomeController.cs b/samples/Mvc.Client/Controllers/HomeController.cs index 91c4b29b..f5d8a4e3 100644 --- a/samples/Mvc.Client/Controllers/HomeController.cs +++ b/samples/Mvc.Client/Controllers/HomeController.cs @@ -3,8 +3,8 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNet.Authorization; -using Microsoft.AspNet.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; namespace Mvc.Client.Controllers { public class HomeController : Controller { diff --git a/samples/Mvc.Client/Properties/launchSettings.json b/samples/Mvc.Client/Properties/launchSettings.json index b5a45a9e..b2eae975 100644 --- a/samples/Mvc.Client/Properties/launchSettings.json +++ b/samples/Mvc.Client/Properties/launchSettings.json @@ -12,12 +12,14 @@ "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { - "ASPNET_ENVIRONMENT": "Development" + "ASPNET_ENV": "Development", + "Hosting:Environment": "Development" } }, "web": { "commandName": "web", "environmentVariables": { + "ASPNET_ENV": "Development", "Hosting:Environment": "Development" } } diff --git a/samples/Mvc.Client/Startup.cs b/samples/Mvc.Client/Startup.cs index 9e9abb67..43b86dcd 100644 --- a/samples/Mvc.Client/Startup.cs +++ b/samples/Mvc.Client/Startup.cs @@ -1,9 +1,8 @@ -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Authentication.Cookies; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Hosting; -using Microsoft.AspNet.Http; -using Microsoft.AspNet.HttpOverrides; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Protocols.OpenIdConnect; @@ -11,8 +10,11 @@ using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Mvc.Client { public class Startup { public static void Main(string[] args) { - var application = new WebApplicationBuilder() - .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + var application = new WebHostBuilder() + .UseCaptureStartupErrors(captureStartupError: true) + .UseDefaultConfiguration(args) + .UseIISPlatformHandlerUrl() + .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseStartup() .Build(); @@ -20,11 +22,10 @@ namespace Mvc.Client { } public void ConfigureServices(IServiceCollection services) { - services.Configure(options => { + services.AddAuthentication(options => { options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }); - services.AddAuthentication(); services.AddMvc(); } @@ -33,45 +34,44 @@ namespace Mvc.Client { factory.AddConsole(); factory.AddDebug(); - app.UseIISPlatformHandler(options => { - options.FlowWindowsAuthentication = false; - }); + app.UseIISPlatformHandler(); - app.UseOverrideHeaders(options => { - options.ForwardedOptions = ForwardedHeaders.All; + app.UseForwardedHeaders(new ForwardedHeadersOptions { + ForwardedHeaders = ForwardedHeaders.All }); + app.UseDeveloperExceptionPage(); + app.UseStaticFiles(); // Insert a new cookies middleware in the pipeline to store the user // identity after he has been redirected from the identity provider. - app.UseCookieAuthentication(options => { - options.AutomaticAuthenticate = true; - options.AutomaticChallenge = true; - options.LoginPath = new PathString("/signin"); + app.UseCookieAuthentication(new CookieAuthenticationOptions { + AutomaticAuthenticate = true, + AutomaticChallenge = true, + LoginPath = new PathString("/signin") }); - app.UseOpenIdConnectAuthentication(options => { + app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { // Note: these settings must match the application details // inserted in the database at the server level. - options.ClientId = "myClient"; - options.ClientSecret = "secret_secret_secret"; - options.PostLogoutRedirectUri = "http://localhost:53507/"; + ClientId = "myClient", + ClientSecret = "secret_secret_secret", + PostLogoutRedirectUri = "http://localhost:53507/", - options.RequireHttpsMetadata = false; - options.GetClaimsFromUserInfoEndpoint = true; - options.SaveTokensAsClaims = true; + RequireHttpsMetadata = false, + GetClaimsFromUserInfoEndpoint = true, + SaveTokensAsClaims = true, // Use the authorization code flow. - options.ResponseType = OpenIdConnectResponseTypes.Code; + ResponseType = OpenIdConnectResponseTypes.Code, // Note: setting the Authority allows the OIDC client middleware to automatically // retrieve the identity provider's configuration and spare you from setting // the different endpoints URIs or the token validation parameters explicitly. - options.Authority = "http://localhost:54540/"; + Authority = "http://localhost:54540/", - options.Scope.Add("email"); - options.Scope.Add("roles"); + Scope = { "email", "roles" } }); diff --git a/samples/Mvc.Client/hosting.json b/samples/Mvc.Client/hosting.json index f159d42d..be5815af 100644 --- a/samples/Mvc.Client/hosting.json +++ b/samples/Mvc.Client/hosting.json @@ -1,3 +1,3 @@ { - "server": "Microsoft.AspNet.Server.Kestrel" + "server": "Microsoft.AspNetCore.Server.Kestrel" } \ No newline at end of file diff --git a/samples/Mvc.Client/project.json b/samples/Mvc.Client/project.json index 1724490c..c9f42a47 100644 --- a/samples/Mvc.Client/project.json +++ b/samples/Mvc.Client/project.json @@ -1,32 +1,40 @@ { "dependencies": { - "Microsoft.AspNet.Mvc": "6.0.0-*", - "Microsoft.AspNet.Authentication.Cookies": "1.0.0-*", - "Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-*", - "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", - "Microsoft.AspNet.HttpOverrides": "1.0.0-*", - "Microsoft.AspNet.Hosting": "1.0.0-*", - "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.StaticFiles": "1.0.0-*", - + "Microsoft.AspNetCore.Mvc": "1.0.0-*", + "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-*", + "Microsoft.AspNetCore.Authentication.OpenIdConnect": "0.1.0-*", + "Microsoft.AspNetCore.Diagnostics": "1.0.0-*", + "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*", + "Microsoft.AspNetCore.HttpOverrides": "1.0.0-*", + "Microsoft.AspNetCore.Hosting": "1.0.0-*", + "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", + "Microsoft.AspNetCore.StaticFiles": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*", "Microsoft.Extensions.Logging.Debug": "1.0.0-*", + "Microsoft.NETCore.Platforms": "1.0.1-*" + }, + + "frameworks": { + "dnx451": { }, - "Newtonsoft.Json": "7.0.1" + "dnxcore50": { + "imports": "portable-net45+win8" + } }, "compilationOptions": { - "emitEntryPoint": true + "emitEntryPoint": true, + "preserveCompilationContext": true }, "commands": { "web": "Mvc.Client" }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { } - }, + "content": [ + "wwwroot", + "Views" + ], "exclude": [ "wwwroot", diff --git a/samples/Mvc.Server/Controllers/AccountController.cs b/samples/Mvc.Server/Controllers/AccountController.cs index 4b2cfd6c..615340c3 100644 --- a/samples/Mvc.Server/Controllers/AccountController.cs +++ b/samples/Mvc.Server/Controllers/AccountController.cs @@ -1,11 +1,10 @@ using System.Linq; using System.Security.Claims; using System.Threading.Tasks; -using Microsoft.AspNet.Authorization; -using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.Data.Entity; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; using Mvc.Server.Models; using Mvc.Server.Services; using Mvc.Server.ViewModels.Account; @@ -168,10 +167,6 @@ namespace Mvc.Server.Controllers { [AllowAnonymous] [ValidateAntiForgeryToken] public async Task ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { - if (User.IsSignedIn()) { - return RedirectToAction(nameof(ManageController.Index), "Manage"); - } - if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); @@ -395,7 +390,7 @@ namespace Mvc.Server.Controllers { } private async Task GetCurrentUserAsync() { - return await _userManager.FindByIdAsync(User.GetUserId()); + return await _userManager.GetUserAsync(User); } private IActionResult RedirectToLocal(string returnUrl) { diff --git a/samples/Mvc.Server/Controllers/HomeController.cs b/samples/Mvc.Server/Controllers/HomeController.cs index 3db07a93..cb57012f 100644 --- a/samples/Mvc.Server/Controllers/HomeController.cs +++ b/samples/Mvc.Server/Controllers/HomeController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNet.Mvc; +using Microsoft.AspNetCore.Mvc; namespace Mvc.Server.Controllers { public class HomeController : Controller { diff --git a/samples/Mvc.Server/Controllers/ManageController.cs b/samples/Mvc.Server/Controllers/ManageController.cs index 0e15abf3..da3f11ac 100644 --- a/samples/Mvc.Server/Controllers/ManageController.cs +++ b/samples/Mvc.Server/Controllers/ManageController.cs @@ -1,9 +1,9 @@ using System.Linq; -using System.Security.Claims; using System.Threading.Tasks; -using Microsoft.AspNet.Authorization; -using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using Mvc.Server.Models; using Mvc.Server.Services; using Mvc.Server.ViewModels.Manage; @@ -15,20 +15,23 @@ namespace Mvc.Server.Controllers { private readonly SignInManager _signInManager; private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; + private readonly ILogger _logger; public ManageController( - UserManager userManager, - SignInManager signInManager, - IEmailSender emailSender, - ISmsSender smsSender) { + UserManager userManager, + SignInManager signInManager, + IEmailSender emailSender, + ISmsSender smsSender, + ILoggerFactory loggerFactory) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _smsSender = smsSender; + _logger = loggerFactory.CreateLogger(); } // - // GET: /Account/Index + // GET: /Manage/Index [HttpGet] public async Task Index(ManageMessageId? message = null) { ViewData["StatusMessage"] = @@ -51,25 +54,15 @@ namespace Mvc.Server.Controllers { return View(model); } - // - // GET: /Account/RemoveLogin - [HttpGet] - public async Task RemoveLogin() { - var user = await GetCurrentUserAsync(); - var linkedAccounts = await _userManager.GetLoginsAsync(user); - ViewData["ShowRemoveButton"] = await _userManager.HasPasswordAsync(user) || linkedAccounts.Count > 1; - return View(linkedAccounts); - } - // // POST: /Manage/RemoveLogin [HttpPost] [ValidateAntiForgeryToken] - public async Task RemoveLogin(string loginProvider, string providerKey) { + public async Task RemoveLogin(RemoveLoginViewModel account) { ManageMessageId? message = ManageMessageId.Error; var user = await GetCurrentUserAsync(); if (user != null) { - var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey); + var result = await _userManager.RemoveLoginAsync(user, account.LoginProvider, account.ProviderKey); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); message = ManageMessageId.RemoveLoginSuccess; @@ -79,13 +72,13 @@ namespace Mvc.Server.Controllers { } // - // GET: /Account/AddPhoneNumber + // GET: /Manage/AddPhoneNumber public IActionResult AddPhoneNumber() { return View(); } // - // POST: /Account/AddPhoneNumber + // POST: /Manage/AddPhoneNumber [HttpPost] [ValidateAntiForgeryToken] public async Task AddPhoneNumber(AddPhoneNumberViewModel model) { @@ -108,6 +101,7 @@ namespace Mvc.Server.Controllers { if (user != null) { await _userManager.SetTwoFactorEnabledAsync(user, true); await _signInManager.SignInAsync(user, isPersistent: false); + _logger.LogInformation(1, "User enabled two-factor authentication."); } return RedirectToAction(nameof(Index), "Manage"); } @@ -121,12 +115,13 @@ namespace Mvc.Server.Controllers { if (user != null) { await _userManager.SetTwoFactorEnabledAsync(user, false); await _signInManager.SignInAsync(user, isPersistent: false); + _logger.LogInformation(2, "User disabled two-factor authentication."); } return RedirectToAction(nameof(Index), "Manage"); } // - // GET: /Account/VerifyPhoneNumber + // GET: /Manage/VerifyPhoneNumber [HttpGet] public async Task VerifyPhoneNumber(string phoneNumber) { var code = await _userManager.GenerateChangePhoneNumberTokenAsync(await GetCurrentUserAsync(), phoneNumber); @@ -135,7 +130,7 @@ namespace Mvc.Server.Controllers { } // - // POST: /Account/VerifyPhoneNumber + // POST: /Manage/VerifyPhoneNumber [HttpPost] [ValidateAntiForgeryToken] public async Task VerifyPhoneNumber(VerifyPhoneNumberViewModel model) { @@ -156,8 +151,9 @@ namespace Mvc.Server.Controllers { } // - // GET: /Account/RemovePhoneNumber - [HttpGet] + // GET: /Manage/RemovePhoneNumber + [HttpPost] + [ValidateAntiForgeryToken] public async Task RemovePhoneNumber() { var user = await GetCurrentUserAsync(); if (user != null) { @@ -178,7 +174,7 @@ namespace Mvc.Server.Controllers { } // - // POST: /Account/Manage + // POST: /Manage/ChangePassword [HttpPost] [ValidateAntiForgeryToken] public async Task ChangePassword(ChangePasswordViewModel model) { @@ -190,6 +186,7 @@ namespace Mvc.Server.Controllers { var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); + _logger.LogInformation(3, "User changed their password successfully."); return RedirectToAction(nameof(Index), new { Message = ManageMessageId.ChangePasswordSuccess }); } AddErrors(result); @@ -227,7 +224,7 @@ namespace Mvc.Server.Controllers { return RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error }); } - //GET: /Account/Manage + //GET: /Manage/ManageLogins [HttpGet] public async Task ManageLogins(ManageMessageId? message = null) { ViewData["StatusMessage"] = @@ -255,8 +252,8 @@ namespace Mvc.Server.Controllers { public IActionResult LinkLogin(string provider) { // Request a redirect to the external login provider to link a login for the current user var redirectUrl = Url.Action("LinkLoginCallback", "Manage"); - var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.GetUserId()); - return new ChallengeResult(provider, properties); + var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User)); + return Challenge(provider, properties); } // @@ -267,7 +264,7 @@ namespace Mvc.Server.Controllers { if (user == null) { return View("Error"); } - var info = await _signInManager.GetExternalLoginInfoAsync(User.GetUserId()); + var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user)); if (info == null) { return RedirectToAction(nameof(ManageLogins), new { Message = ManageMessageId.Error }); } @@ -284,14 +281,6 @@ namespace Mvc.Server.Controllers { } } - private async Task HasPhoneNumber() { - var user = await _userManager.FindByIdAsync(User.GetUserId()); - if (user != null) { - return user.PhoneNumber != null; - } - return false; - } - public enum ManageMessageId { AddPhoneSuccess, AddLoginSuccess, @@ -303,19 +292,10 @@ namespace Mvc.Server.Controllers { Error } - private async Task GetCurrentUserAsync() { - return await _userManager.FindByIdAsync(User.GetUserId()); - } - - private IActionResult RedirectToLocal(string returnUrl) { - if (Url.IsLocalUrl(returnUrl)) { - return Redirect(returnUrl); - } - else { - return RedirectToAction(nameof(HomeController.Index), nameof(HomeController)); - } + private Task GetCurrentUserAsync() { + return _userManager.GetUserAsync(User); } #endregion } -} +} \ No newline at end of file diff --git a/samples/Mvc.Server/Controllers/ResourceController.cs b/samples/Mvc.Server/Controllers/ResourceController.cs index 47a4813f..b27e4222 100644 --- a/samples/Mvc.Server/Controllers/ResourceController.cs +++ b/samples/Mvc.Server/Controllers/ResourceController.cs @@ -1,7 +1,7 @@ using System.Security.Claims; using AspNet.Security.OAuth.Validation; -using Microsoft.AspNet.Authorization; -using Microsoft.AspNet.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; namespace Mvc.Server.Controllers { [Route("api")] diff --git a/samples/Mvc.Server/Models/ApplicationDbContext.cs b/samples/Mvc.Server/Models/ApplicationDbContext.cs index eaf5557f..b4733b60 100644 --- a/samples/Mvc.Server/Models/ApplicationDbContext.cs +++ b/samples/Mvc.Server/Models/ApplicationDbContext.cs @@ -1,4 +1,4 @@ -using Microsoft.Data.Entity; +using Microsoft.EntityFrameworkCore; using OpenIddict; namespace Mvc.Server.Models { diff --git a/samples/Mvc.Server/Models/ApplicationUser.cs b/samples/Mvc.Server/Models/ApplicationUser.cs index 7930cb03..7a885488 100644 --- a/samples/Mvc.Server/Models/ApplicationUser.cs +++ b/samples/Mvc.Server/Models/ApplicationUser.cs @@ -1,7 +1,6 @@ -using Microsoft.AspNet.Identity.EntityFramework; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; namespace Mvc.Server.Models { // Add profile data for application users by adding properties to the ApplicationUser class - public class ApplicationUser : IdentityUser { - } + public class ApplicationUser : IdentityUser { } } diff --git a/samples/Mvc.Server/Properties/launchSettings.json b/samples/Mvc.Server/Properties/launchSettings.json index 8e76a57e..0659c45d 100644 --- a/samples/Mvc.Server/Properties/launchSettings.json +++ b/samples/Mvc.Server/Properties/launchSettings.json @@ -12,12 +12,14 @@ "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { - "ASPNET_ENVIRONMENT": "Development" + "ASPNET_ENV": "Development", + "Hosting:Environment": "Development" } }, "web": { "commandName": "web", "environmentVariables": { + "ASPNET_ENV": "Development", "Hosting:Environment": "Development" } } diff --git a/samples/Mvc.Server/Startup.cs b/samples/Mvc.Server/Startup.cs index 6b1b1da0..b24c9de0 100644 --- a/samples/Mvc.Server/Startup.cs +++ b/samples/Mvc.Server/Startup.cs @@ -1,10 +1,10 @@ using System.Linq; using CryptoHelper; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Hosting; -using Microsoft.AspNet.HttpOverrides; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Data.Entity; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -17,8 +17,11 @@ using OpenIddict.Models; namespace Mvc.Server { public class Startup { public static void Main(string[] args) { - var application = new WebApplicationBuilder() - .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + var application = new WebHostBuilder() + .UseCaptureStartupErrors(captureStartupError: true) + .UseDefaultConfiguration(args) + .UseIISPlatformHandlerUrl() + .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseStartup() .Build(); @@ -52,14 +55,14 @@ namespace Mvc.Server { factory.AddConsole(); factory.AddDebug(); - app.UseIISPlatformHandler(options => { - options.FlowWindowsAuthentication = false; - }); + app.UseIISPlatformHandler(); - app.UseOverrideHeaders(options => { - options.ForwardedOptions = ForwardedHeaders.All; + app.UseForwardedHeaders(new ForwardedHeadersOptions { + ForwardedHeaders = ForwardedHeaders.All }); + app.UseDeveloperExceptionPage(); + app.UseStaticFiles(); // Add a middleware used to validate access @@ -81,14 +84,14 @@ namespace Mvc.Server { app.UseIdentity(); - app.UseGoogleAuthentication(options => { - options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com"; - options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f"; + app.UseGoogleAuthentication(new GoogleOptions { + ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com", + ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f" }); - app.UseTwitterAuthentication(options => { - options.ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g"; - options.ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI"; + app.UseTwitterAuthentication(new TwitterOptions { + ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g", + ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI" }); // Note: OpenIddict must be added after diff --git a/samples/Mvc.Server/ViewModels/Account/SendCodeViewModel.cs b/samples/Mvc.Server/ViewModels/Account/SendCodeViewModel.cs index 5e4018a4..b4507a04 100644 --- a/samples/Mvc.Server/ViewModels/Account/SendCodeViewModel.cs +++ b/samples/Mvc.Server/ViewModels/Account/SendCodeViewModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.Rendering; namespace Mvc.Server.ViewModels.Account { public class SendCodeViewModel { diff --git a/samples/Mvc.Server/ViewModels/Manage/ConfigureTwoFactorViewModel.cs b/samples/Mvc.Server/ViewModels/Manage/ConfigureTwoFactorViewModel.cs index ac20858e..48f8fad6 100644 --- a/samples/Mvc.Server/ViewModels/Manage/ConfigureTwoFactorViewModel.cs +++ b/samples/Mvc.Server/ViewModels/Manage/ConfigureTwoFactorViewModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.Rendering; namespace Mvc.Server.ViewModels.Manage { public class ConfigureTwoFactorViewModel { diff --git a/samples/Mvc.Server/ViewModels/Manage/IndexViewModel.cs b/samples/Mvc.Server/ViewModels/Manage/IndexViewModel.cs index 1b300597..1056f572 100644 --- a/samples/Mvc.Server/ViewModels/Manage/IndexViewModel.cs +++ b/samples/Mvc.Server/ViewModels/Manage/IndexViewModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using Microsoft.AspNet.Identity; +using Microsoft.AspNetCore.Identity; namespace Mvc.Server.ViewModels.Manage { public class IndexViewModel { diff --git a/samples/Mvc.Server/ViewModels/Manage/ManageLoginsViewModel.cs b/samples/Mvc.Server/ViewModels/Manage/ManageLoginsViewModel.cs index bddd3500..23070b10 100644 --- a/samples/Mvc.Server/ViewModels/Manage/ManageLoginsViewModel.cs +++ b/samples/Mvc.Server/ViewModels/Manage/ManageLoginsViewModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Identity; +using Microsoft.AspNetCore.Http.Authentication; +using Microsoft.AspNetCore.Identity; namespace Mvc.Server.ViewModels.Manage { public class ManageLoginsViewModel diff --git a/samples/Mvc.Server/ViewModels/Manage/RemoveLoginViewModel.cs b/samples/Mvc.Server/ViewModels/Manage/RemoveLoginViewModel.cs new file mode 100644 index 00000000..7e8c5838 --- /dev/null +++ b/samples/Mvc.Server/ViewModels/Manage/RemoveLoginViewModel.cs @@ -0,0 +1,6 @@ +namespace Mvc.Server.ViewModels.Manage { + public class RemoveLoginViewModel { + public string LoginProvider { get; set; } + public string ProviderKey { get; set; } + } +} \ No newline at end of file diff --git a/samples/Mvc.Server/Views/Account/Login.cshtml b/samples/Mvc.Server/Views/Account/Login.cshtml index 40b970d7..7721654a 100644 --- a/samples/Mvc.Server/Views/Account/Login.cshtml +++ b/samples/Mvc.Server/Views/Account/Login.cshtml @@ -1,6 +1,6 @@ @using System.Collections.Generic -@using Microsoft.AspNet.Http -@using Microsoft.AspNet.Http.Authentication +@using Microsoft.AspNetCore.Http +@using Microsoft.AspNetCore.Http.Authentication @model LoginViewModel @inject SignInManager SignInManager diff --git a/samples/Mvc.Server/Views/Manage/ManageLogins.cshtml b/samples/Mvc.Server/Views/Manage/ManageLogins.cshtml index 3a809b28..8e77a488 100644 --- a/samples/Mvc.Server/Views/Manage/ManageLogins.cshtml +++ b/samples/Mvc.Server/Views/Manage/ManageLogins.cshtml @@ -1,5 +1,5 @@ @model ManageLoginsViewModel -@using Microsoft.AspNet.Http.Authentication +@using Microsoft.AspNetCore.Http.Authentication @{ ViewData["Title"] = "Manage your external logins"; } diff --git a/samples/Mvc.Server/Views/Manage/RemoveLogin.cshtml b/samples/Mvc.Server/Views/Manage/RemoveLogin.cshtml index 99a57da3..2fdb3ead 100644 --- a/samples/Mvc.Server/Views/Manage/RemoveLogin.cshtml +++ b/samples/Mvc.Server/Views/Manage/RemoveLogin.cshtml @@ -1,4 +1,4 @@ -@model ICollection +@model ICollection @{ ViewData["Title"] = "Remove Login"; } diff --git a/samples/Mvc.Server/Views/Shared/_LoginPartial.cshtml b/samples/Mvc.Server/Views/Shared/_LoginPartial.cshtml index f774bb3c..1f1658bb 100644 --- a/samples/Mvc.Server/Views/Shared/_LoginPartial.cshtml +++ b/samples/Mvc.Server/Views/Shared/_LoginPartial.cshtml @@ -1,20 +1,24 @@ -@using System.Security.Claims +@using Microsoft.AspNetCore.Identity +@using Mvc.Server.Models -@if (User.Identity.IsAuthenticated) -{ +@inject SignInManager SignInManager +@inject UserManager UserManager + +@if (SignInManager.IsSignedIn(User)) { } -else -{ +else { -} +} \ No newline at end of file diff --git a/samples/Mvc.Server/Views/_ViewImports.cshtml b/samples/Mvc.Server/Views/_ViewImports.cshtml index d8fff825..148cc29f 100644 --- a/samples/Mvc.Server/Views/_ViewImports.cshtml +++ b/samples/Mvc.Server/Views/_ViewImports.cshtml @@ -2,5 +2,5 @@ @using Mvc.Server.Models @using Mvc.Server.ViewModels.Account @using Mvc.Server.ViewModels.Manage -@using Microsoft.AspNet.Identity -@addTagHelper *, Microsoft.AspNet.Mvc.TagHelpers +@using Microsoft.AspNetCore.Identity +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/samples/Mvc.Server/hosting.json b/samples/Mvc.Server/hosting.json index f159d42d..be5815af 100644 --- a/samples/Mvc.Server/hosting.json +++ b/samples/Mvc.Server/hosting.json @@ -1,3 +1,3 @@ { - "server": "Microsoft.AspNet.Server.Kestrel" + "server": "Microsoft.AspNetCore.Server.Kestrel" } \ No newline at end of file diff --git a/samples/Mvc.Server/project.json b/samples/Mvc.Server/project.json index 8b35b298..7c0c7706 100644 --- a/samples/Mvc.Server/project.json +++ b/samples/Mvc.Server/project.json @@ -2,36 +2,42 @@ "dependencies": { "AspNet.Security.OAuth.Introspection": "1.0.0-*", "AspNet.Security.OAuth.Validation": "1.0.0-*", - - "EntityFramework.MicrosoftSqlServer": "7.0.0-*", - - "Microsoft.AspNet.Authentication.Google": "1.0.0-*", - "Microsoft.AspNet.Authentication.Twitter": "1.0.0-*", - "Microsoft.AspNet.Diagnostics": "1.0.0-*", - "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", - "Microsoft.AspNet.HttpOverrides": "1.0.0-*", - "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-*", - "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.StaticFiles": "1.0.0-*", - + "Microsoft.AspNetCore.Authentication.Google": "1.0.0-*", + "Microsoft.AspNetCore.Authentication.Twitter": "1.0.0-*", + "Microsoft.AspNetCore.Diagnostics": "1.0.0-*", + "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*", + "Microsoft.AspNetCore.HttpOverrides": "1.0.0-*", + "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-*", + "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", + "Microsoft.AspNetCore.StaticFiles": "1.0.0-*", + "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*", "Microsoft.Extensions.Logging.Debug": "1.0.0-*", - + "Microsoft.NETCore.Platforms": "1.0.1-*", "OpenIddict": "1.0.0-*" }, + "frameworks": { + "dnx451": { }, + + "dnxcore50": { + "imports": "portable-net45+win8" + } + }, + "compilationOptions": { - "emitEntryPoint": true + "emitEntryPoint": true, + "preserveCompilationContext": true }, "commands": { "web": "Mvc.Server" }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { } - }, + "content": [ + "wwwroot", + "Views" + ], "exclude": [ "wwwroot", @@ -45,9 +51,5 @@ "**.xproj", "**.user", "**.vspscc" - ], - - "scripts": { - "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ] - } + ] } \ No newline at end of file diff --git a/src/OpenIddict.Assets/OpenIddictExtensions.cs b/src/OpenIddict.Assets/OpenIddictExtensions.cs index c7fe4abf..a16341e0 100644 --- a/src/OpenIddict.Assets/OpenIddictExtensions.cs +++ b/src/OpenIddict.Assets/OpenIddictExtensions.cs @@ -1,17 +1,21 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ +using System; using System.Reflection; -using Microsoft.AspNet.FileProviders; -using Microsoft.AspNet.StaticFiles; -using Microsoft.Extensions.Internal; +using JetBrains.Annotations; +using Microsoft.Extensions.FileProviders; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static OpenIddictBuilder UseAssets([NotNull] this OpenIddictBuilder builder) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + return builder.AddModule("Assets", -20, app => app.UseStaticFiles(new StaticFileOptions { FileProvider = new EmbeddedFileProvider( assembly: Assembly.Load(new AssemblyName("OpenIddict.Assets")), diff --git a/src/OpenIddict.Assets/project.json b/src/OpenIddict.Assets/project.json index f2236bea..d62ef290 100644 --- a/src/OpenIddict.Assets/project.json +++ b/src/OpenIddict.Assets/project.json @@ -6,24 +6,17 @@ "resource": [ "fonts/*", "scripts/*", "stylesheets/*" ], "dependencies": { - "Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*", - "Microsoft.AspNet.StaticFiles": "1.0.0-*", - - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - }, - + "JetBrains.Annotations": "10.1.2-eap", + "Microsoft.AspNetCore.StaticFiles": "1.0.0-*", + "Microsoft.Extensions.FileProviders.Embedded": "1.0.0-*", "OpenIddict.Core": "1.0.0-*" }, "frameworks": { - "dnx451": { }, + "net451": { }, - "dnxcore50": { - "dependencies": { - "System.Runtime": "4.0.21-*" - } + "dotnet5.4": { + "imports": "portable-net451+win8" } } } \ No newline at end of file diff --git a/src/OpenIddict.Core/IOpenIddictStore.cs b/src/OpenIddict.Core/IOpenIddictStore.cs index b4cd5551..0ec084c7 100644 --- a/src/OpenIddict.Core/IOpenIddictStore.cs +++ b/src/OpenIddict.Core/IOpenIddictStore.cs @@ -1,6 +1,6 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNet.Identity; +using Microsoft.AspNetCore.Identity; namespace OpenIddict { public interface IOpenIddictStore : IUserStore where TUser : class where TApplication : class { diff --git a/src/OpenIddict.Core/OpenIddictBuilder.cs b/src/OpenIddict.Core/OpenIddictBuilder.cs index 87e42042..a7b7fd0b 100644 --- a/src/OpenIddict.Core/OpenIddictBuilder.cs +++ b/src/OpenIddict.Core/OpenIddictBuilder.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using OpenIddict; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { /// /// Holds various properties allowing to configure OpenIddict. /// diff --git a/src/OpenIddict.Core/OpenIddictExtensions.cs b/src/OpenIddict.Core/OpenIddictExtensions.cs index ce930ef4..9c36fbdc 100644 --- a/src/OpenIddict.Core/OpenIddictExtensions.cs +++ b/src/OpenIddict.Core/OpenIddictExtensions.cs @@ -1,25 +1,33 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.Linq; using AspNet.Security.OpenIdConnect.Server; -using Microsoft.AspNet.Hosting; -using Microsoft.AspNet.Identity; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Internal; using OpenIddict; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static IdentityBuilder AddOpenIddictCore( [NotNull] this IdentityBuilder builder, [NotNull] Action configuration) where TApplication : class { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + + if (configuration == null) { + throw new ArgumentNullException(nameof(configuration)); + } + builder.Services.AddAuthentication(); builder.Services.AddCaching(); @@ -49,6 +57,18 @@ namespace Microsoft.AspNet.Builder { [NotNull] this OpenIddictBuilder builder, [NotNull] string name, int position, [NotNull] Action registration) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(name)) { + throw new ArgumentNullException(nameof(name)); + } + + if (registration == null) { + throw new ArgumentNullException(nameof(registration)); + } + // Note: always call ToArray to make sure the foreach // block doesn't iterate on the modified collection. foreach (var module in builder.Modules.Where(module => string.Equals(module.Name, name)).ToArray()) { @@ -71,6 +91,14 @@ namespace Microsoft.AspNet.Builder { public static IApplicationBuilder UseOpenIddictCore( [NotNull] this IApplicationBuilder app, [NotNull] Action configuration) { + if (app == null) { + throw new ArgumentNullException(nameof(app)); + } + + if (configuration == null) { + throw new ArgumentNullException(nameof(configuration)); + } + var builder = new OpenIddictBuilder(); // Resolve the OpenIddict provider from the services container. diff --git a/src/OpenIddict.Core/OpenIddictManager.cs b/src/OpenIddict.Core/OpenIddictManager.cs index d387f666..a5eff439 100644 --- a/src/OpenIddict.Core/OpenIddictManager.cs +++ b/src/OpenIddict.Core/OpenIddictManager.cs @@ -2,32 +2,36 @@ using System.Collections.Generic; using System.Linq; using System.Security.Claims; +using System.Threading; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; using CryptoHelper; -using Microsoft.AspNet.Http; -using Microsoft.AspNet.Identity; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace OpenIddict { public class OpenIddictManager : UserManager where TUser : class where TApplication : class { - public OpenIddictManager([NotNull] IServiceProvider services) - : base(services: services, - store: services.GetService>(), - optionsAccessor: services.GetService>(), - passwordHasher: services.GetService>(), - userValidators: services.GetServices>(), - passwordValidators: services.GetServices>(), - keyNormalizer: services.GetService(), - errors: services.GetService(), - logger: services.GetService>>(), - contextAccessor: services.GetService()) { - Context = services.GetRequiredService().HttpContext; - Options = services.GetRequiredService>().Value; + public OpenIddictManager( + IOpenIddictStore store, + IOptions optionsAccessor, + IPasswordHasher passwordHasher, + IEnumerable> userValidators, + IEnumerable> passwordValidators, + ILookupNormalizer keyNormalizer, + IdentityErrorDescriber errors, + IServiceProvider services, + ILogger> logger) + : base(store, optionsAccessor, + passwordHasher, userValidators, + passwordValidators, keyNormalizer, + errors, services, logger) { + Context = services.GetService()?.HttpContext; + Options = optionsAccessor.Value; } /// @@ -35,6 +39,11 @@ namespace OpenIddict { /// public virtual HttpContext Context { get; } + /// + /// Gets the cancellation token used to abort async operations. + /// + public virtual CancellationToken CancellationToken => Context?.RequestAborted ?? CancellationToken.None; + /// /// Gets the Identity options associated with the current manager. /// @@ -112,11 +121,11 @@ namespace OpenIddict { } public virtual Task FindApplicationByIdAsync(string identifier) { - return Store.FindApplicationByIdAsync(identifier, Context.RequestAborted); + return Store.FindApplicationByIdAsync(identifier, CancellationToken); } public virtual Task FindApplicationByLogoutRedirectUri(string url) { - return Store.FindApplicationByLogoutRedirectUri(url, Context.RequestAborted); + return Store.FindApplicationByLogoutRedirectUri(url, CancellationToken); } public virtual async Task FindClaimAsync(TUser user, string type) { @@ -141,7 +150,7 @@ namespace OpenIddict { throw new ArgumentNullException(nameof(application)); } - var type = await Store.GetApplicationTypeAsync(application, Context.RequestAborted); + var type = await Store.GetApplicationTypeAsync(application, CancellationToken); // Ensure the application type returned by the store is supported by the manager. if (!string.Equals(type, OpenIddictConstants.ApplicationTypes.Confidential, StringComparison.OrdinalIgnoreCase) && @@ -158,7 +167,7 @@ namespace OpenIddict { throw new ArgumentNullException(nameof(application)); } - return Store.GetDisplayNameAsync(application, Context.RequestAborted); + return Store.GetDisplayNameAsync(application, CancellationToken); } public virtual async Task ValidateRedirectUriAsync(TApplication application, string address) { @@ -166,7 +175,7 @@ namespace OpenIddict { throw new ArgumentNullException(nameof(application)); } - if (!string.Equals(address, await Store.GetRedirectUriAsync(application, Context.RequestAborted), StringComparison.Ordinal)) { + if (!string.Equals(address, await Store.GetRedirectUriAsync(application, CancellationToken), StringComparison.Ordinal)) { Logger.LogWarning("Client validation failed because {RedirectUri} was not a valid redirect_uri " + "for {Client}", address, await GetDisplayNameAsync(application)); @@ -187,7 +196,7 @@ namespace OpenIddict { return false; } - var hash = await Store.GetHashedSecretAsync(application, Context.RequestAborted); + var hash = await Store.GetHashedSecretAsync(application, CancellationToken); if (string.IsNullOrEmpty(hash)) { Logger.LogError("Client authentication failed for {Client} because " + "no client secret was associated with the application."); diff --git a/src/OpenIddict.Core/OpenIddictModule.cs b/src/OpenIddict.Core/OpenIddictModule.cs index a8bac69f..af799f47 100644 --- a/src/OpenIddict.Core/OpenIddictModule.cs +++ b/src/OpenIddict.Core/OpenIddictModule.cs @@ -1,5 +1,5 @@ using System; -using Microsoft.AspNet.Builder; +using Microsoft.AspNetCore.Builder; namespace OpenIddict { /// diff --git a/src/OpenIddict.Core/OpenIddictOptions.cs b/src/OpenIddict.Core/OpenIddictOptions.cs index 3ec60ca0..185ceae5 100644 --- a/src/OpenIddict.Core/OpenIddictOptions.cs +++ b/src/OpenIddict.Core/OpenIddictOptions.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ diff --git a/src/OpenIddict.Core/OpenIddictProvider.Authentication.cs b/src/OpenIddict.Core/OpenIddictProvider.Authentication.cs index 334fe80c..318fe263 100644 --- a/src/OpenIddict.Core/OpenIddictProvider.Authentication.cs +++ b/src/OpenIddict.Core/OpenIddictProvider.Authentication.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ @@ -12,10 +12,10 @@ using System.Security.Claims; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Http.Authentication; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http.Authentication; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; namespace OpenIddict { public partial class OpenIddictProvider : OpenIdConnectServerProvider where TUser : class where TApplication : class { @@ -68,7 +68,7 @@ namespace OpenIddict { // the appropriate set of scopes is requested to prevent personal data leakage. if (context.HttpContext.User.Identities.Any(identity => identity.IsAuthenticated)) { // Ensure the user profile still exists in the database. - var user = await manager.FindByIdAsync(context.HttpContext.User.GetUserId()); + var user = await manager.GetUserAsync(context.HttpContext.User); if (user == null) { context.Reject( error: OpenIdConnectConstants.Errors.ServerError, @@ -147,7 +147,7 @@ namespace OpenIddict { // the initial check made by ValidateAuthorizationRequest. // In this case, ignore the prompt=none request and // continue to the next middleware in the pipeline. - var user = await manager.FindByIdAsync(principal.GetUserId()); + var user = await manager.GetUserAsync(principal); if (user == null) { return; } diff --git a/src/OpenIddict.Core/OpenIddictProvider.Exchange.cs b/src/OpenIddict.Core/OpenIddictProvider.Exchange.cs index 59e045d6..f41f9292 100644 --- a/src/OpenIddict.Core/OpenIddictProvider.Exchange.cs +++ b/src/OpenIddict.Core/OpenIddictProvider.Exchange.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ @@ -10,11 +10,11 @@ using System.Security.Claims; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Identity; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http.Authentication; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.Options; namespace OpenIddict { @@ -128,10 +128,10 @@ namespace OpenIddict { var manager = context.HttpContext.RequestServices.GetRequiredService>(); var options = context.HttpContext.RequestServices.GetRequiredService>(); - var principal = context.AuthenticationTicket?.Principal; + var principal = context.Ticket?.Principal; Debug.Assert(principal != null); - var user = await manager.FindByIdAsync(principal.GetUserId()); + var user = await manager.GetUserAsync(principal); if (user == null) { context.Reject( error: OpenIdConnectConstants.Errors.InvalidGrant, @@ -156,14 +156,14 @@ namespace OpenIddict { // Note: the "scopes" property stored in context.AuthenticationTicket is automatically // updated by ASOS when the client application requests a restricted scopes collection. - var identity = await manager.CreateIdentityAsync(user, context.AuthenticationTicket.GetScopes()); + var identity = await manager.CreateIdentityAsync(user, context.Ticket.GetScopes()); Debug.Assert(identity != null); // Create a new authentication ticket holding the user identity but // reuse the authentication properties stored in the refresh token. var ticket = new AuthenticationTicket( new ClaimsPrincipal(identity), - context.AuthenticationTicket.Properties, + context.Ticket.Properties, context.Options.AuthenticationScheme); context.Validate(ticket); diff --git a/src/OpenIddict.Core/OpenIddictProvider.Introspection.cs b/src/OpenIddict.Core/OpenIddictProvider.Introspection.cs index 5d75957b..cc38a4a8 100644 --- a/src/OpenIddict.Core/OpenIddictProvider.Introspection.cs +++ b/src/OpenIddict.Core/OpenIddictProvider.Introspection.cs @@ -1,18 +1,17 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.Diagnostics; -using System.Security.Claims; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; -using Microsoft.AspNet.Identity; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.Options; namespace OpenIddict { @@ -82,10 +81,10 @@ namespace OpenIddict { return; } - var principal = context.AuthenticationTicket?.Principal; + var principal = context.Ticket?.Principal; Debug.Assert(principal != null); - var user = await manager.FindByIdAsync(principal.GetUserId()); + var user = await manager.GetUserAsync(principal); if (user == null) { context.Active = false; diff --git a/src/OpenIddict.Core/OpenIddictProvider.Session.cs b/src/OpenIddict.Core/OpenIddictProvider.Session.cs index 9d47f45b..478a593f 100644 --- a/src/OpenIddict.Core/OpenIddictProvider.Session.cs +++ b/src/OpenIddict.Core/OpenIddictProvider.Session.cs @@ -1,14 +1,14 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; +using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; namespace OpenIddict { public partial class OpenIddictProvider : OpenIdConnectServerProvider where TUser : class where TApplication : class { diff --git a/src/OpenIddict.Core/OpenIddictProvider.cs b/src/OpenIddict.Core/OpenIddictProvider.cs index 3ad2c5dc..c89e113e 100644 --- a/src/OpenIddict.Core/OpenIddictProvider.cs +++ b/src/OpenIddict.Core/OpenIddictProvider.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ @@ -9,6 +9,7 @@ using System.Security.Claims; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; using AspNet.Security.OpenIdConnect.Server; +using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Internal; using Newtonsoft.Json.Linq; @@ -30,12 +31,12 @@ namespace OpenIddict { public override async Task ProfileEndpoint([NotNull] ProfileEndpointContext context) { var manager = context.HttpContext.RequestServices.GetRequiredService>(); - var principal = context.AuthenticationTicket?.Principal; + var principal = context.Ticket?.Principal; Debug.Assert(principal != null); // Note: user may be null if the user has been removed. // In this case, return a 400 response. - var user = await manager.FindByIdAsync(principal.GetUserId()); + var user = await manager.GetUserAsync(principal); if (user == null) { context.Response.StatusCode = 400; context.HandleResponse(); @@ -51,7 +52,7 @@ namespace OpenIddict { // Note: filtering the username is not needed at this stage as OpenIddictController.Accept // and OpenIddictProvider.GrantResourceOwnerCredentials are expected to reject requests that // don't include the "email" scope if the username corresponds to the registed email address. - if (context.AuthenticationTicket.HasScope(OpenIdConnectConstants.Scopes.Profile)) { + if (context.Ticket.HasScope(OpenIdConnectConstants.Scopes.Profile)) { context.PreferredUsername = await manager.GetUserNameAsync(user); if (manager.SupportsUserClaim) { @@ -62,7 +63,7 @@ namespace OpenIddict { } // Only add the email address details if the "email" scope was present in the access token. - if (context.AuthenticationTicket.HasScope(OpenIdConnectConstants.Scopes.Email)) { + if (context.Ticket.HasScope(OpenIdConnectConstants.Scopes.Email)) { context.Email = await manager.GetEmailAsync(user); // Only add the "email_verified" claim @@ -73,7 +74,7 @@ namespace OpenIddict { }; // Only add the phone number details if the "phone" scope was present in the access token. - if (context.AuthenticationTicket.HasScope(OpenIdConnectConstants.Scopes.Phone)) { + if (context.Ticket.HasScope(OpenIdConnectConstants.Scopes.Phone)) { context.PhoneNumber = await manager.GetPhoneNumberAsync(user); // Only add the "phone_number_verified" @@ -84,7 +85,7 @@ namespace OpenIddict { } // Only add the roles list if the "roles" scope was present in the access token. - if (manager.SupportsUserRole && context.AuthenticationTicket.HasScope(OpenIddictConstants.Scopes.Roles)) { + if (manager.SupportsUserRole && context.Ticket.HasScope(OpenIddictConstants.Scopes.Roles)) { var roles = await manager.GetRolesAsync(user); if (roles.Count != 0) { context.Claims[OpenIddictConstants.Claims.Roles] = JArray.FromObject(roles); diff --git a/src/OpenIddict.Core/OpenIddictServices.cs b/src/OpenIddict.Core/OpenIddictServices.cs index 35bfa3e6..222212c2 100644 --- a/src/OpenIddict.Core/OpenIddictServices.cs +++ b/src/OpenIddict.Core/OpenIddictServices.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ diff --git a/src/OpenIddict.Core/project.json b/src/OpenIddict.Core/project.json index ba071634..0d0ba42c 100644 --- a/src/OpenIddict.Core/project.json +++ b/src/OpenIddict.Core/project.json @@ -4,25 +4,20 @@ "description": "Core components of OpenIddict.", "dependencies": { - "Microsoft.AspNet.Cors": "6.0.0-*", - "Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*", - "Microsoft.AspNet.Identity": "3.0.0-*", - - "Microsoft.Extensions.Configuration": "1.0.0-*", - "Microsoft.Extensions.Caching.Memory": "1.0.0-*", - - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - }, - "AspNet.Security.OpenIdConnect.Server": "1.0.0-*", - - "CryptoHelper": "1.0.0-rc2-*" + "CryptoHelper": "1.0.0-rc2-*", + "JetBrains.Annotations": "10.1.2-eap", + "Microsoft.AspNetCore.Cors": "1.0.0-*", + "Microsoft.AspNetCore.Identity": "1.0.0-*", + "Microsoft.Extensions.Configuration": "1.0.0-*", + "Microsoft.Extensions.Caching.Memory": "1.0.0-*" }, "frameworks": { - "dnx451": { }, - "dnxcore50": { } + "net451": { }, + + "dotnet5.4": { + "imports": "portable-net451+win8" + } } } \ No newline at end of file diff --git a/src/OpenIddict.EF/OpenIddictContext.cs b/src/OpenIddict.EF/OpenIddictContext.cs index 776e77f8..45012696 100644 --- a/src/OpenIddict.EF/OpenIddictContext.cs +++ b/src/OpenIddict.EF/OpenIddictContext.cs @@ -1,12 +1,12 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Data.Entity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using OpenIddict.Models; namespace OpenIddict { diff --git a/src/OpenIddict.EF/OpenIddictExtensions.cs b/src/OpenIddict.EF/OpenIddictExtensions.cs index 424bdb8b..b436a8ad 100644 --- a/src/OpenIddict.EF/OpenIddictExtensions.cs +++ b/src/OpenIddict.EF/OpenIddictExtensions.cs @@ -1,22 +1,26 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.Linq; using System.Reflection; -using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Data.Entity; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; using OpenIddict; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static OpenIddictServices UseEntityFramework([NotNull] this OpenIddictServices services) { + if (services == null) { + throw new ArgumentNullException(nameof(services)); + } + services.Services.AddScoped( typeof(IOpenIddictStore<,>).MakeGenericType(services.UserType, services.ApplicationType), typeof(OpenIddictStore<,,,,>).MakeGenericType( diff --git a/src/OpenIddict.EF/OpenIddictStore.cs b/src/OpenIddict.EF/OpenIddictStore.cs index 92f6c689..23e838f0 100644 --- a/src/OpenIddict.EF/OpenIddictStore.cs +++ b/src/OpenIddict.EF/OpenIddictStore.cs @@ -1,8 +1,8 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Data.Entity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using OpenIddict.Models; namespace OpenIddict { diff --git a/src/OpenIddict.EF/project.json b/src/OpenIddict.EF/project.json index 8515cfde..f8d02fa2 100644 --- a/src/OpenIddict.EF/project.json +++ b/src/OpenIddict.EF/project.json @@ -4,19 +4,17 @@ "description": "Entity Framework adapter for OpenIddict.", "dependencies": { - "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-*", - - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - }, - + "JetBrains.Annotations": "10.1.2-eap", + "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-*", "OpenIddict.Core": "1.0.0-*", "OpenIddict.Models": "1.0.0-*" }, "frameworks": { - "dnx451": { }, - "dnxcore50": { } + "net451": { }, + + "dotnet5.4": { + "imports": "portable-net451+win8" + } } } \ No newline at end of file diff --git a/src/OpenIddict.Models/Application.cs b/src/OpenIddict.Models/Application.cs index cc9d01ca..99a985b3 100644 --- a/src/OpenIddict.Models/Application.cs +++ b/src/OpenIddict.Models/Application.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ diff --git a/src/OpenIddict.Models/project.json b/src/OpenIddict.Models/project.json index 732f511c..6d528cb8 100644 --- a/src/OpenIddict.Models/project.json +++ b/src/OpenIddict.Models/project.json @@ -4,11 +4,14 @@ "description": "Contains the default models used by OpenIddict.", "frameworks": { - "dnx451": { }, - "dnxcore50": { + "net451": { }, + + "dotnet5.4": { "dependencies": { - "System.Runtime": "4.0.21-*" - } + "System.Runtime": "4.1.0-*" + }, + + "imports": "portable-net451+win8" } } } \ No newline at end of file diff --git a/src/OpenIddict.Mvc/OpenIddictController.cs b/src/OpenIddict.Mvc/OpenIddictController.cs index 92ca328c..69b430fd 100644 --- a/src/OpenIddict.Mvc/OpenIddictController.cs +++ b/src/OpenIddict.Mvc/OpenIddictController.cs @@ -1,6 +1,6 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ @@ -11,12 +11,12 @@ using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using AspNet.Security.OpenIdConnect.Extensions; -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Authorization; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Mvc; -using Microsoft.Extensions.Internal; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http.Authentication; +using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace OpenIddict.Mvc { @@ -119,7 +119,7 @@ namespace OpenIddict.Mvc { } // Retrieve the user data using the unique identifier. - var user = await Manager.FindByIdAsync(User.GetUserId()); + var user = await Manager.GetUserAsync(User); if (user == null) { return View("Error", new OpenIdConnectMessage { Error = OpenIdConnectConstants.Errors.ServerError, @@ -245,7 +245,7 @@ namespace OpenIddict.Mvc { // Instruct the cookies middleware to delete the local cookie created // when the user agent is redirected from the external identity provider // after a successful authentication flow (e.g Google or Facebook). - await HttpContext.Authentication.SignOutAsync("Microsoft.AspNet.Identity.Application"); + await HttpContext.Authentication.SignOutAsync("Microsoft.AspNetCore.Identity.Application"); // This call will instruct AspNet.Security.OpenIdConnect.Server to serialize // the specified identity to build appropriate tokens (id_token and token). diff --git a/src/OpenIddict.Mvc/OpenIddictExtensions.cs b/src/OpenIddict.Mvc/OpenIddictExtensions.cs index cf5f751d..41ef2d7d 100644 --- a/src/OpenIddict.Mvc/OpenIddictExtensions.cs +++ b/src/OpenIddict.Mvc/OpenIddictExtensions.cs @@ -1,24 +1,28 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.Diagnostics; using System.Reflection; -using Microsoft.AspNet.FileProviders; -using Microsoft.AspNet.Http; -using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Mvc.ApplicationModels; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; +using Microsoft.Extensions.FileProviders; using OpenIddict; using OpenIddict.Mvc; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static OpenIddictBuilder UseMvc([NotNull] this OpenIddictBuilder builder) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + // Run MVC in an isolated environment. return builder.AddModule("MVC", 10, app => app.Isolate(map => map.UseMvc(routes => { // Register the actions corresponding to the authorization endpoint. @@ -56,13 +60,11 @@ namespace Microsoft.AspNet.Builder { .AddMvcOptions(options => options.Conventions.Add(new OpenIddictConvention())) .AddRazorOptions(options => { - // Update the Razor options to also use a combined provider that + // Update the Razor options to also use an embedded file provider that // falls back to the current assembly when searching for views. - options.FileProvider = new CompositeFileProvider( - options.FileProvider, - new EmbeddedFileProvider( - assembly: typeof(OpenIddictController<,>).GetTypeInfo().Assembly, - baseNamespace: typeof(OpenIddictController<,>).Namespace)); + options.FileProviders.Add(new EmbeddedFileProvider( + assembly: typeof(OpenIddictController<,>).GetTypeInfo().Assembly, + baseNamespace: typeof(OpenIddictController<,>).Namespace)); }); // Register the sign-in manager in the isolated container. @@ -85,6 +87,12 @@ namespace Microsoft.AspNet.Builder { return container.GetRequiredService(typeof(OpenIddictManager<,>).MakeGenericType(registration.UserType, registration.ApplicationType)); }); + // Register the user manager in the isolated container. + services.AddScoped(typeof(UserManager<>).MakeGenericType(registration.UserType), provider => { + return provider.GetRequiredService(typeof(OpenIddictManager<,>) + .MakeGenericType(registration.UserType, registration.ApplicationType)); + }); + // Register the options in the isolated container. services.AddScoped(provider => builder.Options); })); diff --git a/src/OpenIddict.Mvc/Views/Shared/SignIn.cshtml b/src/OpenIddict.Mvc/Views/Shared/SignIn.cshtml index 8613a6c5..f1c0ac8c 100644 --- a/src/OpenIddict.Mvc/Views/Shared/SignIn.cshtml +++ b/src/OpenIddict.Mvc/Views/Shared/SignIn.cshtml @@ -1,4 +1,4 @@ -@using Microsoft.AspNet.Http.Authentication +@using Microsoft.AspNetCore.Http.Authentication @model IEnumerable
diff --git a/src/OpenIddict.Mvc/project.json b/src/OpenIddict.Mvc/project.json index 5168cbe2..c5259a6d 100644 --- a/src/OpenIddict.Mvc/project.json +++ b/src/OpenIddict.Mvc/project.json @@ -7,25 +7,18 @@ "dependencies": { "AspNet.Hosting.Extensions": "1.0.0-*", - - "Microsoft.AspNet.FileProviders.Composite": "1.0.0-*", - "Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*", - "Microsoft.AspNet.Mvc": "6.0.0-*", - - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - }, - + "JetBrains.Annotations": "10.1.2-eap", + "Microsoft.AspNetCore.Mvc": "1.0.0-*", + "Microsoft.Extensions.FileProviders.Embedded": "1.0.0-*", + "Microsoft.Extensions.FileProviders.Composite": "1.0.0-*", "OpenIddict.Core": "1.0.0-*" }, "frameworks": { - "dnx451": { }, - "dnxcore50": { - "dependencies": { - "System.Reflection": "4.1.0-*" - } + "net451": { }, + + "dotnet5.6": { + "imports": "portable-net451+win8" } } } \ No newline at end of file diff --git a/src/OpenIddict.Security/OpenIddictExtensions.cs b/src/OpenIddict.Security/OpenIddictExtensions.cs index b5335eb4..d1199240 100644 --- a/src/OpenIddict.Security/OpenIddictExtensions.cs +++ b/src/OpenIddict.Security/OpenIddictExtensions.cs @@ -1,10 +1,14 @@ using System; -using Microsoft.Extensions.Internal; +using JetBrains.Annotations; using NWebsec.Middleware; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static OpenIddictBuilder UseNWebsec([NotNull] this OpenIddictBuilder builder) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + return builder.UseNWebsec(options => { options.DefaultSources(directive => directive.Self()) .ImageSources(directive => directive.Self().CustomSources("*")) @@ -16,6 +20,14 @@ namespace Microsoft.AspNet.Builder { public static OpenIddictBuilder UseNWebsec( [NotNull] this OpenIddictBuilder builder, [NotNull] Action configuration) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + + if (configuration == null) { + throw new ArgumentNullException(nameof(configuration)); + } + return builder.AddModule("NWebsec", 5, app => { // Insert a new middleware responsible of setting the Content-Security-Policy header. // See https://nwebsec.codeplex.com/wikipage?title=Configuring%20Content%20Security%20Policy&referringTitle=NWebsec @@ -36,7 +48,10 @@ namespace Microsoft.AspNet.Builder { } public static OpenIddictBuilder UseCors([NotNull] this OpenIddictBuilder builder) { - //Add CORS to the app + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + builder.AddModule("CORS", -10, map => map.UseCors(options => { options.AllowAnyHeader(); options.AllowAnyMethod(); diff --git a/src/OpenIddict.Security/project.json b/src/OpenIddict.Security/project.json index 32fa44fa..c7551494 100644 --- a/src/OpenIddict.Security/project.json +++ b/src/OpenIddict.Security/project.json @@ -4,19 +4,20 @@ "description": "Security headers module for OpenIddict.", "dependencies": { + "JetBrains.Annotations": "10.1.2-eap", "OpenIddict.Core": "1.0.0-*", + "NWebsec": { "type": "build", "version": "1.0.0-internal-*" - }, - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" } }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { } - } + "frameworks": { + "net451": { }, + + "dotnet5.4": { + "imports": "portable-net451+win8" + } + } } diff --git a/src/OpenIddict/OpenIddictExtensions.cs b/src/OpenIddict/OpenIddictExtensions.cs index 59238a36..4dff0ac5 100644 --- a/src/OpenIddict/OpenIddictExtensions.cs +++ b/src/OpenIddict/OpenIddictExtensions.cs @@ -1,17 +1,21 @@ /* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/core for more information concerning + * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; -using Microsoft.AspNet.Identity; -using Microsoft.Extensions.Internal; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Identity; using OpenIddict.Models; -namespace Microsoft.AspNet.Builder { +namespace Microsoft.AspNetCore.Builder { public static class OpenIddictExtensions { public static IdentityBuilder AddOpenIddict([NotNull] this IdentityBuilder builder) { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + return builder.AddOpenIddictCore(configuration => { // Use the EF adapter by default. configuration.UseEntityFramework(); @@ -20,6 +24,10 @@ namespace Microsoft.AspNet.Builder { public static IdentityBuilder AddOpenIddict([NotNull] this IdentityBuilder builder) where TApplication : Application { + if (builder == null) { + throw new ArgumentNullException(nameof(builder)); + } + return builder.AddOpenIddictCore(configuration => { // Use the EF adapter by default. configuration.UseEntityFramework(); @@ -33,6 +41,14 @@ namespace Microsoft.AspNet.Builder { public static IApplicationBuilder UseOpenIddict( [NotNull] this IApplicationBuilder app, [NotNull] Action configuration) { + if (app == null) { + throw new ArgumentNullException(nameof(app)); + } + + if (configuration == null) { + throw new ArgumentNullException(nameof(configuration)); + } + return app.UseOpenIddictCore(builder => { builder.UseAssets(); builder.UseCors(); diff --git a/src/OpenIddict/project.json b/src/OpenIddict/project.json index 6380531a..5cd7f71d 100644 --- a/src/OpenIddict/project.json +++ b/src/OpenIddict/project.json @@ -4,19 +4,18 @@ "description": "Easy-to-use OpenID Connect server for ASP.NET 5.", "dependencies": { + "JetBrains.Annotations": "10.1.2-eap", "OpenIddict.Assets": "1.0.0-*", "OpenIddict.EF": "1.0.0-*", "OpenIddict.Mvc": "1.0.0-*", - "OpenIddict.Security": "1.0.0-*", - - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - } + "OpenIddict.Security": "1.0.0-*" }, "frameworks": { - "dnx451": { }, - "dnxcore50": { } + "net451": { }, + + "dotnet5.6": { + "imports": "portable-net451+win8" + } } } \ No newline at end of file