From b3de11f8d320777315eb7ee465c7c75c33807c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 1 Mar 2018 11:00:31 +0300 Subject: [PATCH] Login/logout is now working for the microservice auth server demo. --- .../MicroserviceDemo.AuthServer.csproj | 1 + .../MicroservicesAuthServerModule.cs | 16 ++++++++++++++- .../Pages/Index.cshtml | 20 +++++++++++++++++++ .../Pages/Index.cshtml.cs | 17 ++++++++++++++++ .../MicroservicesDemoWebModule.cs | 11 +++++++++- .../Pages/Account/Login.cshtml | 2 +- .../Pages/Account/Login.cshtml.cs | 17 +++++++++++++++- 7 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml create mode 100644 src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml.cs diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj index 3661de16dd..2ffe2ec9c0 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj @@ -17,6 +17,7 @@ + diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs index 1fe43dc686..21e54e30f6 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs @@ -58,6 +58,8 @@ namespace MicroserviceDemo.AuthServer }); } + services.AddAuthorization(); + services.AddAssemblyOf(); } @@ -73,7 +75,19 @@ namespace MicroserviceDemo.AuthServer app.UseStaticFiles(); app.UseVirtualFiles(); - app.UseMvcWithDefaultRoute(); + app.UseAuthentication(); + + app.UseMvc(routes => + { + //TODO: Can we make an extension method for adding these two routes inside the framework? + routes.MapRoute( + name: "defaultWithArea", + template: "{area}/{controller=Home}/{action=Index}/{id?}"); + + routes.MapRoute( + name: "default", + template: "{controller=Home}/{action=Index}/{id?}"); + }); } private static IConfigurationRoot BuildConfiguration(IHostingEnvironment env) diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml new file mode 100644 index 0000000000..0712e36914 --- /dev/null +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml @@ -0,0 +1,20 @@ +@page +@model MicroserviceDemo.AuthServer.Pages.IndexModel + +

Welcome to the Authentication Server!

+ + + + + Login + + @if (User.Identity.IsAuthenticated) + { + foreach (var claim in User.Claims) + { +
@claim.Type = @claim.Value
+ } + } + +
+
\ No newline at end of file diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml.cs b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml.cs new file mode 100644 index 0000000000..0771c699c0 --- /dev/null +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/Pages/Index.cshtml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace MicroserviceDemo.AuthServer.Pages +{ + public class IndexModel : PageModel + { + public void OnGet() + { + + } + } +} \ No newline at end of file diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs index 7b91f3de5a..e2cc2c8ca3 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs @@ -109,7 +109,16 @@ namespace MicroserviceDemo.Web app.UseAuthentication(); - app.UseMvcWithDefaultRoute(); + app.UseMvc(routes => + { + routes.MapRoute( + name: "defaultWithArea", + template: "{area}/{controller=Home}/{action=Index}/{id?}"); + + routes.MapRoute( + name: "default", + template: "{controller=Home}/{action=Index}/{id?}"); + }); } private static IConfigurationRoot BuildConfiguration(IHostingEnvironment env) diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml index 75cef4ee5e..7e45ba5176 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml +++ b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml @@ -26,7 +26,7 @@ - + @* TODO: Only show if identity server is used *@
diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs index e71030e3b8..e280fcd7ce 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs +++ b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs @@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using IdentityServer4.Models; using IdentityServer4.Services; using IdentityServer4.Stores; using Microsoft.AspNetCore.Authentication; @@ -112,8 +113,20 @@ namespace Volo.Abp.Account.Web.Pages.Account } [UnitOfWork] //TODO: Will be removed when we implement action filter - public virtual async Task OnPostAsync() + public virtual async Task OnPostAsync(string action) { + if (action == "Cancel") + { + var context = await _interaction.GetAuthorizationContextAsync(ReturnUrl); + if (context == null) + { + return RedirectSafely("~/"); + } + + await _interaction.GrantConsentAsync(context, ConsentResponse.Denied); + return RedirectSafely(ReturnUrl); + } + ValidateModel(); var result = await _signInManager.PasswordSignInAsync( @@ -128,6 +141,8 @@ namespace Volo.Abp.Account.Web.Pages.Account throw new UserFriendlyException("Login failed!"); //TODO: Handle other cases, do not throw exception } + //var user = _userManager.FindByIdAsync(result.) + return RedirectSafely(ReturnUrl, ReturnUrlHash); }