From 43af3278a3cc9275d592b2eb1f99532beffe840c Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 20 Apr 2021 10:12:55 +0800 Subject: [PATCH 1/7] Update Authorization.md --- docs/en/Authorization.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/docs/en/Authorization.md b/docs/en/Authorization.md index 270345f444..2fdc59c36b 100644 --- a/docs/en/Authorization.md +++ b/docs/en/Authorization.md @@ -405,17 +405,14 @@ public class SocialSecurityNumberClaimsPrincipalContributor : IAbpClaimsPrincipa public async Task ContributeAsync(AbpClaimsPrincipalContributorContext context) { var identity = context.ClaimsPrincipal.Identities.FirstOrDefault(); - if (identity != null) + var userId = identity?.FindUserId(); + if (userId.HasValue) { - var currentUser = context.ServiceProvider.GetRequiredService(); - if (currentUser.Id.HasValue) + var userService = context.ServiceProvider.GetRequiredService(); //Your custom service + var socialSecurityNumber = await userService.GetSocialSecurityNumberAsync(userId.Value); + if (socialSecurityNumber != null) { - var userService = context.ServiceProvider.GetRequiredService(); //Your custom service - var socialSecurityNumber = await userService.GetSocialSecurityNumberAsync(currentUser.Id.Value); - if (userService != null) - { - identity.AddOrReplace(new Claim("SocialSecurityNumber", userService)); - } + identity.AddOrReplace(new Claim("SocialSecurityNumber", socialSecurityNumber)); } } } From f710c454d97334272c74b8c5256f8ce776512fee Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 20 Apr 2021 10:55:41 +0800 Subject: [PATCH 2/7] Added Swagger to Blazor-Server projects --- .../MyProjectNameBlazorModule.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs index 9d860a9030..ef3225458a 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; using MyCompanyName.MyProjectName.Blazor.Server.Menus; using MyCompanyName.MyProjectName.EntityFrameworkCore; using MyCompanyName.MyProjectName.Localization; @@ -86,6 +87,7 @@ namespace MyCompanyName.MyProjectName.Blazor.Server ConfigureAutoMapper(); ConfigureVirtualFileSystem(hostingEnvironment); ConfigureLocalizationServices(); + ConfigureSwaggerServices(context.Services); ConfigureAutoApiControllers(); ConfigureHttpClient(context); ConfigureBlazorise(context); @@ -186,6 +188,18 @@ namespace MyCompanyName.MyProjectName.Blazor.Server }); } + private void ConfigureSwaggerServices(IServiceCollection services) + { + services.AddSwaggerGen( + options => + { + options.SwaggerDoc("v1", new OpenApiInfo { Title = "MyProjectName API", Version = "v1" }); + options.DocInclusionPredicate((docName, description) => true); + options.CustomSchemaIds(type => type.FullName); + } + ); + } + private static void ConfigureHttpClient(ServiceConfigurationContext context) { context.Services.AddTransient(sp => new HttpClient @@ -265,6 +279,11 @@ namespace MyCompanyName.MyProjectName.Blazor.Server app.UseUnitOfWork(); app.UseIdentityServer(); app.UseAuthorization(); + app.UseSwagger(); + app.UseAbpSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProjectName API"); + }); app.UseConfiguredEndpoints(); } } From b6a553dc1a6ae7468337ac2cedf53c977fc63697 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 20 Apr 2021 23:25:57 +0300 Subject: [PATCH 3/7] handle oauth erros --- .../src/lib/strategies/auth-flow.strategy.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts index 82f0ac413a..7cc3abddac 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts @@ -2,14 +2,21 @@ import { HttpHeaders } from '@angular/common/http'; import { Injector } from '@angular/core'; import { Params, Router } from '@angular/router'; import { Store } from '@ngxs/store'; -import { AuthConfig, OAuthInfoEvent, OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; +import { + AuthConfig, + OAuthErrorEvent, + OAuthInfoEvent, + OAuthService, + OAuthStorage, +} from 'angular-oauth2-oidc'; import { from, Observable, of } from 'rxjs'; import { filter, switchMap, tap } from 'rxjs/operators'; import { RestOccurError } from '../actions/rest.actions'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; -import { SessionStateService } from '../services/session-state.service'; import { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; +import { SessionStateService } from '../services/session-state.service'; +import { noop } from '../utils/common-utils'; export interface LoginParams { username: string; @@ -28,6 +35,8 @@ export abstract class AuthFlowStrategy { protected configState: ConfigStateService; protected oAuthService: OAuthService; protected oAuthConfig: AuthConfig; + protected appConfigService: AbpApplicationConfigurationService; + abstract checkIfInternalAuth(): boolean; abstract navigateToLogin(queryParams?: Params): void; abstract logout(): Observable; @@ -40,7 +49,10 @@ export abstract class AuthFlowStrategy { this.environment = injector.get(EnvironmentService); this.configState = injector.get(ConfigStateService); this.oAuthService = injector.get(OAuthService); + this.appConfigService = injector.get(AbpApplicationConfigurationService); this.oAuthConfig = this.environment.getEnvironment().oAuthConfig; + + this.listenToOauthErrors(); } async init(): Promise { @@ -66,6 +78,18 @@ export abstract class AuthFlowStrategy { protected refreshToken() { return this.oAuthService.refreshToken().catch(() => clearOAuthStorage()); } + + protected listenToOauthErrors() { + this.oAuthService.events + .pipe( + filter(event => event instanceof OAuthErrorEvent), + tap(() => clearOAuthStorage()), + switchMap(() => this.appConfigService.get()), + ) + .subscribe(res => { + this.configState.setState(res); + }); + } } export class AuthCodeFlowStrategy extends AuthFlowStrategy { @@ -74,7 +98,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { async init() { return super .init() - .then(() => this.oAuthService.tryLogin()) + .then(() => this.oAuthService.tryLogin().catch(noop)) .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token')); } @@ -101,7 +125,6 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = true; private cookieKey = 'rememberMe'; private storageKey = 'passwordFlow'; - private appConfigService = this.injector.get(AbpApplicationConfigurationService); private listenToTokenExpiration() { this.oAuthService.events From ca786e7dc5b9ebaa66aa7e95d442dc6e17e04db6 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 20 Apr 2021 23:29:33 +0300 Subject: [PATCH 4/7] add notFound value to prevent injection error --- npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts index c4edc800b5..449be29e7e 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts @@ -41,7 +41,7 @@ export function getInitialData(injector: Injector) { injector.get(SessionStateService).setTenant(currentTenant); }), catchError(error => { - const appInitErrorHandlers = injector.get(APP_INIT_ERROR_HANDLERS); + const appInitErrorHandlers = injector.get(APP_INIT_ERROR_HANDLERS, null); if (appInitErrorHandlers && appInitErrorHandlers.length) { appInitErrorHandlers.forEach(fn => fn(error)); } From 1777b144817d591df863bc5f694cda123e3fd0a4 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 20 Apr 2021 23:29:43 +0300 Subject: [PATCH 5/7] add allowSyntheticDefaultImports to tsconfig.json --- npm/ng-packs/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/npm/ng-packs/tsconfig.json b/npm/ng-packs/tsconfig.json index 75ff03b711..ca40efc770 100644 --- a/npm/ng-packs/tsconfig.json +++ b/npm/ng-packs/tsconfig.json @@ -7,6 +7,7 @@ "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, "module": "esnext", "moduleResolution": "node", "importHelpers": true, From 4c3cbbd00cc7ee43aac414891f8ed8191d5ce11a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 20 Apr 2021 23:39:43 +0300 Subject: [PATCH 6/7] resolve lint error --- npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts index 449be29e7e..87982d8bd0 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts @@ -43,7 +43,7 @@ export function getInitialData(injector: Injector) { catchError(error => { const appInitErrorHandlers = injector.get(APP_INIT_ERROR_HANDLERS, null); if (appInitErrorHandlers && appInitErrorHandlers.length) { - appInitErrorHandlers.forEach(fn => fn(error)); + appInitErrorHandlers.forEach(func => func(error)); } return throwError(error); From a7353e58f657f5fb01bc6163feecaa79f750824a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 21 Apr 2021 01:22:37 +0300 Subject: [PATCH 7/7] Fixed duplicate setting menu problem MVC UI doesn't add the setting menu if blazor server UI is being used. --- .../Menus/SettingManagementMenuContributor.cs | 7 +++++-- .../SettingManagementMainMenuContributor.cs | 13 +++++++++---- .../Pages/SettingManagement/Index.cshtml.cs | 1 - 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs index a3a18ffb33..5070cef3a6 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs @@ -21,8 +21,7 @@ namespace Volo.Abp.SettingManagement.Blazor.Menus { var settingManagementPageOptions = context.ServiceProvider.GetRequiredService>().Value; var settingPageCreationContext = new SettingComponentCreationContext(context.ServiceProvider); - if ( - !settingManagementPageOptions.Contributors.Any() || + if (!settingManagementPageOptions.Contributors.Any() || !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext)) ) { @@ -31,6 +30,10 @@ namespace Volo.Abp.SettingManagement.Blazor.Menus var l = context.GetLocalizer(); + /* This may happen if MVC UI is being used in the same application. + * In this case, we are removing the MVC setting management UI. */ + context.Menu.GetAdministration().TryRemoveMenuItem(SettingManagementMenus.GroupName); + context.Menu .GetAdministration() .AddItem( diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs index 20946c09ea..59fc10c174 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs @@ -19,12 +19,17 @@ namespace Volo.Abp.SettingManagement.Web.Navigation return; } + if (context.Menu.FindMenuItem(SettingManagementMenuNames.GroupName) != null) + { + /* This may happen if blazor server UI is being used in the same application. + * In this case, we don't add the MVC setting management UI. */ + return; + } + var settingManagementPageOptions = context.ServiceProvider.GetRequiredService>().Value; var settingPageCreationContext = new SettingPageCreationContext(context.ServiceProvider); - if ( - !settingManagementPageOptions.Contributors.Any() || - !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext)) - ) + if (!settingManagementPageOptions.Contributors.Any() || + !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext))) { return; } diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs index 87eb2e5bf4..8cc7f62143 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Mvc;