This commit is contained in:
2024-11-05 08:28:42 +01:00
parent 4ecf802db3
commit 8ab7ab8c38
24 changed files with 1240 additions and 1992 deletions

View File

@@ -3,12 +3,8 @@ Imports System.Net.Http
Imports System.Security.Principal
Imports System.Threading
Imports System.Web.Http.Controllers
Imports System.Web.Http.Description
Imports System.Web.Http.Filters
Imports Microsoft.AspNetCore.Authorization
Imports Swashbuckle.Swagger
Imports VERAG_PROG_ALLGEMEIN
Imports VERAG_PROG_ALLGEMEIN.TESTJSON
@@ -25,6 +21,7 @@ Public Class BasicAuthenticationAttribute
Dim usernamePasswordArray = decodedAuthenticationToken.Split(":"c)
Dim userName = usernamePasswordArray(0)
Dim password = usernamePasswordArray(1)
'Dim hashedPW = BCrypt.Net.BCrypt.HashPassword(password)
@@ -51,9 +48,9 @@ Public Class BasicAuthenticationAttribute
Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
Dim authenticated As Boolean = False
Dim Response = SQL.DLookup("username", "tblAuthentication", "username='" & user & "' and password='" & password & "'", "FMZOLL", "")
If Response <> "" Then
authenticated = True
Dim hashedPassword = SQL.DLookup("hashedPassword", "tblRESTAuthentication", "username='" & user & "' AND type = 'REST'", "ADMIN", "")
If hashedPassword <> "" Then
authenticated = BCrypt.Net.BCrypt.Verify(password, hashedPassword)
End If
Return authenticated

View File

@@ -26,24 +26,25 @@ Public Class SwaggerConfig
Dim commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML"
Dim commentsFile = Path.Combine(baseDirectory, commentsFileName)
GlobalConfiguration.Configuration.EnableSwagger(Function(c)
GlobalConfiguration.Configuration.EnableSwagger(Function(c) As SwaggerDocsConfig
c.PrettyPrint()
c.MultipleApiVersions(Function(apiDesc, targetApiVersion) ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion), Function(vc)
c.MultipleApiVersions(Function(apiDesc, targetApiVersion) ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion), Function(vc) As VersionInfoBuilder
vc.Version("v1", descr & " V1").Description("A sample API for testing ").TermsOfService("Some Terms").Contact(Function(cont)
cont.Name("VERAG AG")
cont.Url("http://test.at")
cont.Email("Mail.test")
End Function).License(Function(lic)
lic.Name("Lizenz")
lic.Url("http://test.at")
End Function)
vc.Version("1", descr & " V1").Description("A sample API for testing ").TermsOfService("Some Terms").Contact(Function(cont) As ContactBuilder
cont.Name("VERAG AG")
cont.Url("http://test.at")
cont.Email("Mail.test")
End Function).License(Function(lic)
lic.Name("Lizenz")
lic.Url("http://test.at")
End Function)
vc.Version("v2", descr & " V2")
vc.Version("v3", descr & " V3")
vc.Version("2", descr & " V2")
vc.Version("3", descr & " V3")
Return vc
End Function)
@@ -59,8 +60,8 @@ Public Class SwaggerConfig
'c.OperationFilter(Of BasicAuthenticationAttribute)()
End Function).EnableSwaggerUi(Function(c)
Return c
End Function).EnableSwaggerUi(Function(c) As SwaggerUiConfig
c.DocumentTitle(descr)
c.EnableDiscoveryUrlSelector()
@@ -68,13 +69,13 @@ Public Class SwaggerConfig
c.CustomAsset("index", thisAssembly, "VERAG_REST_SERVER.my_index.html")
Return c
End Function)
End Sub
@@ -85,15 +86,27 @@ Public Class SwaggerConfig
End Function
Public Shared Sub Register(ByVal config As HttpConfiguration)
RouteTable.Routes.MapOwinPath("swagger", Function(app) As Owin.IAppBuilder
Return app.UseSwaggerUi3(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUi3Settings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{id}"
Return settings
End Function)
app.UseSwaggerUi(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUiSettings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{id}"
settings.DocumentTitle = descr
settings.DocExpansion = "list"
settings.DefaultModelsExpandDepth = -1
Return settings
End Function)
Return app
End Function)
End Sub
End Class

View File

@@ -73,14 +73,14 @@ Public Module WebApiConfig
config.AddApiVersioning(Function(options)
options.ReportApiVersions = True
options.AssumeDefaultVersionWhenUnspecified = True
options.ApiVersionReader = ApiVersionReader.Combine(New UrlSegmentApiVersionReader(),
New HeaderApiVersionReader("x-api-version"),
New MediaTypeApiVersionReader("x-api-version"))
'options.DefaultApiVersion = New ApiVersion("1")
End Function)
'config.AddApiVersioning(Function(options)
' options.ReportApiVersions = True
' options.AssumeDefaultVersionWhenUnspecified = True
' options.ApiVersionReader = ApiVersionReader.Combine(New UrlSegmentApiVersionReader(),
' New HeaderApiVersionReader("x-api-version"),
' New MediaTypeApiVersionReader("x-api-version"))
' 'options.DefaultApiVersion = New ApiVersion("1")
' End Function)
config.AddVersionedApiExplorer(Function(c)
@@ -98,15 +98,50 @@ Public Module WebApiConfig
Public Sub Register(ByVal config As HttpConfiguration)
SwaggerConfig.Register(config)
Dim constraintsResolver = New DefaultInlineConstraintResolver()
constraintsResolver.ConstraintMap.Add("apiVersion", GetType(ApiVersionRouteConstraint))
config.MapHttpAttributeRoutes(constraintsResolver)
'config.Filters.Add(New AuthorizeAttribute) 'wenn alle API Aufrufe autorisiert werden müssen/ansonsten können einzelene Controlleraufrufe auth. werden!
config.Filters.Add(New BasicAuthenticationAttribute())
'config.AddApiVersioning(Sub(options As Action(Of ApiVersioningOptions)
' '// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
' options.ReportApiVersions = True
' options.AssumeDefaultVersionWhenUnspecified = True
' options.ApiVersionReader = ApiVersionReader.Combine(New UrlSegmentApiVersionReader(),
' New HeaderApiVersionReader("x-api-version"),
' New MediaTypeApiVersionReader("x-api-version"))
' End Sub)
config.AddApiVersioning(Sub(options As Microsoft.Web.Http.Versioning.ApiVersioningOptions)
options.AssumeDefaultVersionWhenUnspecified = True
options.ReportApiVersions = True
options.DefaultApiVersion = New Microsoft.Web.Http.ApiVersion(1, 0)
options.DefaultApiVersion = New Microsoft.Web.Http.ApiVersion(2, 0)
options.ApiVersionSelector = New Microsoft.Web.Http.Versioning.LowestImplementedApiVersionSelector(options)
End Sub)
config.AddVersionedApiExplorer(Function(c)
c.GroupNameFormat = "'v'VVV"
c.SubstituteApiVersionInUrl = True
End Function)
If False Then 'wenn alle API Aufrufe autorisiert werden müssen/ansonsten können einzelene Controlleraufrufe auth. werden!
config.Filters.Add(New AuthorizeAttribute)
Else
config.Filters.Add(New BasicAuthenticationAttribute())
End If
SwaggerConfig.Register(config)
End Sub