From a6099b10fd59e3fea4473d2390664e39b7bf6ad5 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Sat, 28 Mar 2020 05:30:01 +0300 Subject: [PATCH 1/2] added post --- docs/en/Blog-Posts/2020-04-02/Post.md | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 docs/en/Blog-Posts/2020-04-02/Post.md diff --git a/docs/en/Blog-Posts/2020-04-02/Post.md b/docs/en/Blog-Posts/2020-04-02/Post.md new file mode 100644 index 0000000000..a2df997091 --- /dev/null +++ b/docs/en/Blog-Posts/2020-04-02/Post.md @@ -0,0 +1,74 @@ +# Custom SignIn Manager + +ABP Framework uses Microsoft Identity underneath hence supports customization as much as Microsoft Identity does. + +### Creating CustomSignInManager + +To create your own custom SignIn Manager, you need to inherit `SignInManager`. + +````xml +public class CustomSignInManager : SignInManager +{ + public CustomSigninManager( + UserManager userManager, + IHttpContextAccessor contextAccessor, + IUserClaimsPrincipalFactory claimsFactory, + IOptions optionsAccessor, + ILogger> logger, + IAuthenticationSchemeProvider schemes, + IUserConfirmation confirmation) + : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation) + { + } +} +```` + +### Overriding Methods + +Afterwards you can override a method like `GetExternalLoginInfoAsync`: + +````xml +public override async Task GetExternalLoginInfoAsync(string expectedXsrf = null) +{ + var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme); + var items = auth?.Properties?.Items; + if (auth?.Principal == null || items == null || !items.ContainsKey("LoginProvider")) + { + return null; + } + + if (expectedXsrf != null) + { + if (!items.ContainsKey("XsrfId")) + { + return null; + } + var userId = items[XsrfKey] as string; + if (userId != expectedXsrf) + { + return null; + } + var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier); + var provider = items[LoginProviderKey] as string; + if (providerKey == null || provider == null) + { + return null; + } + + var providerDisplayName = (await GetExternalAuthenticationSchemesAsync()).FirstOrDefault(p => p.Name == provider)?.DisplayName ?? provider; + return new ExternalLoginInfo(auth.Principal, provider, providerKey, providerDisplayName) + { + AuthenticationTokens = auth.Properties.GetTokens() + }; +} +```` + +### Registering to DI + +You need to register your Custom SignIn Manager to DI to activate it. Inside the `.Web` project, locate the `ApplicationNameWebModule` add the following under `ConfigureServices` method: + +````xml +context.Services +.GetObject() + .AddSignInManager(); +```` \ No newline at end of file From 90c3a36ec11add5c048aeffd1747e82a7ee6b24a Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Sat, 28 Mar 2020 16:17:04 +0300 Subject: [PATCH 2/2] updated headers and added sample link --- docs/en/Blog-Posts/2020-04-02/Post.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/en/Blog-Posts/2020-04-02/Post.md b/docs/en/Blog-Posts/2020-04-02/Post.md index a2df997091..7919d49032 100644 --- a/docs/en/Blog-Posts/2020-04-02/Post.md +++ b/docs/en/Blog-Posts/2020-04-02/Post.md @@ -1,8 +1,18 @@ # Custom SignIn Manager +## Introduction + ABP Framework uses Microsoft Identity underneath hence supports customization as much as Microsoft Identity does. -### Creating CustomSignInManager + + +## Sample Code + +https://github.com/abpframework/abp-samples/blob/master/aspnet-core/BookStore-AzureAD/src/Acme.BookStore.Web/CustomSignInManager.cs + + + +## Creating CustomSignInManager To create your own custom SignIn Manager, you need to inherit `SignInManager`. @@ -23,7 +33,9 @@ public class CustomSignInManager : SignInManager } ```` -### Overriding Methods + + +## Overriding Methods Afterwards you can override a method like `GetExternalLoginInfoAsync`: @@ -63,9 +75,11 @@ public override async Task GetExternalLoginInfoAsync(string e } ```` -### Registering to DI -You need to register your Custom SignIn Manager to DI to activate it. Inside the `.Web` project, locate the `ApplicationNameWebModule` add the following under `ConfigureServices` method: + +## Registering to DI + +You need to register your Custom SignIn Manager to DI to activate it. Inside the `.Web` project, locate the `ApplicationNameWebModule` and add the following under `ConfigureServices` method: ````xml context.Services