48 lines
1.7 KiB
VB.net
48 lines
1.7 KiB
VB.net
Imports System.Net
|
|
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
|
|
|
|
Public Class BasicAuthenticationAttribute
|
|
Inherits AuthorizationFilterAttribute
|
|
|
|
Public Overrides Sub OnAuthorization(ByVal actionContext As HttpActionContext)
|
|
Dim authHeader = actionContext.Request.Headers.Authorization
|
|
|
|
If authHeader IsNot Nothing Then
|
|
Dim authenticationToken = actionContext.Request.Headers.Authorization.Parameter
|
|
Dim decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken))
|
|
Dim usernamePasswordArray = decodedAuthenticationToken.Split(":"c)
|
|
Dim userName = usernamePasswordArray(0)
|
|
Dim password = usernamePasswordArray(1)
|
|
Dim isValid = userName = "test" AndAlso password = "password"
|
|
|
|
If isValid Then
|
|
Dim principal = New GenericPrincipal(New GenericIdentity(userName), Nothing)
|
|
Thread.CurrentPrincipal = principal
|
|
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, "User " & userName & " successfully authenticated")
|
|
Return
|
|
End If
|
|
End If
|
|
|
|
HandleUnathorized(actionContext)
|
|
End Sub
|
|
|
|
Private Shared Sub HandleUnathorized(ByVal actionContext As HttpActionContext)
|
|
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized)
|
|
actionContext.Response.Headers.Add("WWW-Authenticate", "Basic Scheme='Data' location = 'http://localhost:")
|
|
End Sub
|
|
End Class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|