Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
ProjectsStatusManagement
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.bahjat
ProjectsStatusManagement
Commits
e8959c8b
Commit
e8959c8b
authored
Sep 07, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add cors via option pattern
parent
24de3ed6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
82 additions
and
19 deletions
+82
-19
BuilderConfigurations.cs
PSManagement.Api/Configurations/BuilderConfigurations.cs
+22
-0
DependencyInjection.cs
PSManagement.Api/DependencyInjection/DependencyInjection.cs
+16
-17
CorsSettings.cs
PSManagement.Api/Settings/CorsSettings.cs
+21
-0
Startup.cs
PSManagement.Api/Startup.cs
+8
-2
appsettings.json
PSManagement.Api/appsettings.json
+15
-0
No files found.
PSManagement.Api/Configurations/BuilderConfigurations.cs
0 → 100644
View file @
e8959c8b
using
Microsoft.AspNetCore.Builder
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
PSManagement.Api.Configurations
{
public
static
class
BuilderConfigurations
{
#
region
Use
Cors
Configure
public
static
IApplicationBuilder
UseMyCors
(
this
IApplicationBuilder
app
)
{
app
.
UseCors
(
"AllowHiast"
);
return
app
;
}
#
endregion
Use
Cors
Configure
}
}
PSManagement.Api/DependencyInjection/DependencyInjection.cs
View file @
e8959c8b
...
@@ -6,17 +6,18 @@ using System.Reflection;
...
@@ -6,17 +6,18 @@ using System.Reflection;
using
Microsoft.OpenApi.Models
;
using
Microsoft.OpenApi.Models
;
using
System
;
using
System
;
using
AutoMapper
;
using
AutoMapper
;
using
PSManagement.Api.Settings
;
namespace
PSManagement.Api.DI
namespace
PSManagement.Api.DI
{
{
public
static
class
DependencyInjection
public
static
class
DependencyInjection
{
{
public
static
IServiceCollection
AddAPI
(
this
IServiceCollection
services
)
public
static
IServiceCollection
AddAPI
(
this
IServiceCollection
services
,
IConfiguration
configuration
)
{
{
services
services
.
AddApiSwagger
()
.
AddApiSwagger
()
.
AddApiCors
()
.
AddApiCors
(
configuration
)
.
AddMyMiddlewares
()
.
AddMyMiddlewares
()
;
;
...
@@ -61,25 +62,23 @@ namespace PSManagement.Api.DI
...
@@ -61,25 +62,23 @@ namespace PSManagement.Api.DI
#
endregion
Api
Docs
Swagger
#
endregion
Api
Docs
Swagger
#
region
Cors
#
region
Cors
private
static
IServiceCollection
AddApiCors
(
this
IServiceCollection
services
)
private
static
IServiceCollection
AddApiCors
(
this
IServiceCollection
services
,
IConfiguration
configuration
)
{
{
services
.
Configure
<
CorsSettings
>(
configuration
.
GetSection
(
CorsSettings
.
SectionName
));
services
.
AddCors
(
options
=>
services
.
AddCors
(
options
=>
{
{
CorsSettings
corsSettings
=
configuration
.
GetSection
(
CorsSettings
.
SectionName
).
Get
<
CorsSettings
>();
foreach
(
Policy
policy
in
corsSettings
.
Policies
)
{
options
.
AddPolicy
(
policy
.
PolicyName
,
builder
=>
builder
.
WithOrigins
(
policy
.
AllowedOrigins
)
// Add your frontend URL here
.
AllowAnyHeader
()
.
AllowAnyMethod
()
.
AllowCredentials
());
options
.
AddPolicy
(
"AllowFrontend"
,
}
builder
=>
builder
.
WithOrigins
(
"http://localhost:4200"
)
// Add your frontend URL here
.
AllowAnyHeader
()
.
AllowAnyMethod
()
.
AllowCredentials
());
options
.
AddPolicy
(
"AllowHiast"
,
builder
=>
builder
.
WithOrigins
(
"**.hiast.edu.sy/"
)
// Add your frontend URL here
.
AllowAnyHeader
()
.
AllowAnyMethod
()
.
AllowCredentials
());
});
});
...
...
PSManagement.Api/Settings/CorsSettings.cs
0 → 100644
View file @
e8959c8b
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
PSManagement.Api.Settings
{
public
class
CorsSettings
{
public
const
string
SectionName
=
"CorsSettings"
;
public
Policy
[]
Policies
{
get
;
set
;
}
=
null
!;
}
public
class
Policy
{
public
string
PolicyName
{
get
;
set
;
}
public
string
AllowedOrigins
{
get
;
set
;
}
}
}
PSManagement.Api/Startup.cs
View file @
e8959c8b
...
@@ -16,6 +16,8 @@ using PSManagement.Infrastructure.Persistence.DI;
...
@@ -16,6 +16,8 @@ using PSManagement.Infrastructure.Persistence.DI;
using
PSManagement.Api.DI
;
using
PSManagement.Api.DI
;
using
PSManagement.Presentation.DependencyInjection
;
using
PSManagement.Presentation.DependencyInjection
;
using
PSManagement.Api.Middleware.ExceptionHandler
;
using
PSManagement.Api.Middleware.ExceptionHandler
;
using
PSManagement.Api.Settings
;
using
PSManagement.Api.Configurations
;
namespace
PSManagement.Api
namespace
PSManagement.Api
{
{
...
@@ -34,7 +36,7 @@ namespace PSManagement.Api
...
@@ -34,7 +36,7 @@ namespace PSManagement.Api
{
{
// adding dependency injection
// adding dependency injection
services
services
.
AddAPI
()
.
AddAPI
(
Configuration
)
.
AddPresentation
()
.
AddPresentation
()
.
AddApplication
()
.
AddApplication
()
.
AddPersistence
(
Configuration
)
.
AddPersistence
(
Configuration
)
...
@@ -59,7 +61,9 @@ namespace PSManagement.Api
...
@@ -59,7 +61,9 @@ namespace PSManagement.Api
app
.
UseHttpsRedirection
();
app
.
UseHttpsRedirection
();
app
.
UseStaticFiles
();
app
.
UseStaticFiles
();
app
.
UseRouting
();
app
.
UseRouting
();
app
.
UseCors
(
"AllowFrontend"
);
app
.
UseMyCors
();
app
.
UseAuthentication
();
app
.
UseAuthentication
();
app
.
UseAuthorization
();
app
.
UseAuthorization
();
app
.
UseEndpoints
(
endpoints
=>
app
.
UseEndpoints
(
endpoints
=>
...
@@ -67,5 +71,7 @@ namespace PSManagement.Api
...
@@ -67,5 +71,7 @@ namespace PSManagement.Api
endpoints
.
MapControllers
();
endpoints
.
MapControllers
();
});
});
}
}
}
}
}
}
PSManagement.Api/appsettings.json
View file @
e8959c8b
...
@@ -42,5 +42,20 @@
...
@@ -42,5 +42,20 @@
},
},
"FileServiceSettings"
:
{
"FileServiceSettings"
:
{
"AvailableExtension"
:
[
".png"
,
".pdf"
]
"AvailableExtension"
:
[
".png"
,
".pdf"
]
},
"CorsSettings"
:
{
"Policies"
:
[
{
"PolicyName"
:
"AllowHiast"
,
"AllowedOrigins"
:
"http://*.hiast.edu.sy/"
},
{
"PolicyName"
:
"AllowFrontend"
,
"AllowedOrigins"
:
"http://localhost:4200"
}
]
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment