From 0cc656f62771c94d2ea6fff9999b8f7ebbab9384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 1 Mar 2018 14:06:53 +0300 Subject: [PATCH] Handle Windows external login. --- .../Pages/Account/Login.cshtml | 16 +++--- .../Pages/Account/Login.cshtml.cs | 55 ++++++++++++++++++- src/Volo.Abp.Account.Web/AbpAccountOptions.cs | 3 + 3 files changed, 63 insertions(+), 11 deletions(-) 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 7e45ba5176..61364f038d 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml +++ b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml @@ -1,7 +1,7 @@ @page @model Volo.Abp.Account.Web.Pages.Account.IdsLoginModel
- + @if (Model.EnableLocalLogin) {
@@ -41,16 +41,16 @@

Use another service to log in.

-
- @foreach (var provider in Model.VisibleExternalProviders) - { - - } -
+ + + @foreach (var provider in Model.VisibleExternalProviders) + { + + }
} - + @if (!Model.EnableLocalLogin && !Model.VisibleExternalProviders.Any()) {
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 ecc1abfbcf..03b67acf27 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,7 +4,9 @@ using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Security.Claims; +using System.Security.Principal; using System.Threading.Tasks; +using IdentityModel; using IdentityServer4.Events; using IdentityServer4.Models; using IdentityServer4.Services; @@ -164,13 +166,18 @@ namespace Volo.Abp.Account.Web.Pages.Account } [UnitOfWork] - public virtual IActionResult OnPostExternalLogin(string provider, string returnUrl = "", string returnUrlHash = "") + public virtual async Task OnPostExternalLogin(string provider) { - var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { returnUrl, returnUrlHash }); + if (_accountOptions.WindowsAuthenticationSchemeName == provider) + { + return await ProcessWindowsLoginAsync(); + } + var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); + properties.Items["scheme"] = provider; - return new ChallengeResult(provider, properties); + return Challenge(properties, provider); } [UnitOfWork] @@ -234,6 +241,48 @@ namespace Volo.Abp.Account.Web.Pages.Account return user; } + private async Task ProcessWindowsLoginAsync() + { + var result = await HttpContext.AuthenticateAsync(_accountOptions.WindowsAuthenticationSchemeName); + if (!(result?.Principal is WindowsPrincipal windowsPrincipal)) + { + return Challenge(_accountOptions.WindowsAuthenticationSchemeName); + } + + var props = new AuthenticationProperties + { + RedirectUri = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }), + Items = + { + {"scheme", _accountOptions.WindowsAuthenticationSchemeName}, + } + }; + + var identity = new ClaimsIdentity(_accountOptions.WindowsAuthenticationSchemeName); + identity.AddClaim(new Claim(JwtClaimTypes.Subject, windowsPrincipal.Identity.Name)); + identity.AddClaim(new Claim(JwtClaimTypes.Name, windowsPrincipal.Identity.Name)); + + //TODO: Consider to add Windows groups the the identity + //if (_accountOptions.IncludeWindowsGroups) + //{ + // var windowsIdentity = windowsPrincipal.Identity as WindowsIdentity; + // if (windowsIdentity != null) + // { + // var groups = windowsIdentity.Groups?.Translate(typeof(NTAccount)); + // var roles = groups.Select(x => new Claim(JwtClaimTypes.Role, x.Value)); + // identity.AddClaims(roles); + // } + //} + + await HttpContext.SignInAsync( + IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme, + new ClaimsPrincipal(identity), + props + ); + + return RedirectSafely(props.RedirectUri); + } + public class LoginInputModel { [Required] diff --git a/src/Volo.Abp.Account.Web/AbpAccountOptions.cs b/src/Volo.Abp.Account.Web/AbpAccountOptions.cs index e814ee44f5..21e72155cc 100644 --- a/src/Volo.Abp.Account.Web/AbpAccountOptions.cs +++ b/src/Volo.Abp.Account.Web/AbpAccountOptions.cs @@ -2,6 +2,9 @@ { public class AbpAccountOptions { + /// + /// Default value: . + /// public string WindowsAuthenticationSchemeName { get; set; } public AbpAccountOptions()