Explorar o código

fix auth fail when 0

Robert hai 1 ano
pai
achega
6a1d861ec3

+ 1 - 0
EVCB_OCPP.WSServer/Service/HeaderRecordService.cs

@@ -55,6 +55,7 @@ namespace HeaderRecord
         internal void LogResponse(string traceIdentifier, HttpResponse response)
         {
             logger.LogInformation("LogResponse============================================================");
+            logger.LogInformation("{id} {key} {value}", traceIdentifier, "statuscode", response.StatusCode);
             foreach (var headerKey in response.Headers.Keys)
             {
                 logger.LogInformation("{id} {key} {value}", traceIdentifier, headerKey, response.Headers[headerKey]);

+ 1 - 1
EVCB_OCPP.WSServer/Service/MapApiServce.cs

@@ -131,7 +131,7 @@ public static class MapApiServceExtention
         routeHandlerBuilder.AddEndpointFilter(async (context, next) =>
         {
             string key = context.HttpContext.Request.Headers["key"];
-            if (!string.IsNullOrEmpty(key) &&
+            if (!string.IsNullOrEmpty(pass) &&
                 key != pass)
             {
                 return Results.BadRequest();

+ 10 - 5
EVCB_OCPP.WSServer/Service/WsService/OcppWebsocketService.cs

@@ -156,8 +156,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
 
         if (!isExistedSN)
         {
+            logger.LogTrace("{id} {func} not found", context.TraceIdentifier, nameof(ValidateHandshake));
             context.Response.StatusCode = StatusCodes.Status404NotFound;
-            logger.LogTrace("{id} {func} {Statuscode}", context.TraceIdentifier, nameof(ValidateHandshake), context.Response.StatusCode);
             return false;
         }
 
@@ -182,10 +182,15 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
             securityProfile = 0;
         }
 
+        if (securityProfile == 0)
+        {
+            authorizated = true;
+        }
+
         if (securityProfile == 3 && session.UriScheme == "ws")
         {
+            logger.LogTrace("{id} {func} {securityProfile} auth failed", context.TraceIdentifier, nameof(ValidateHandshake), securityProfile);
             context.Response.StatusCode = StatusCodes.Status401Unauthorized;
-            logger.LogTrace("{id} {func} {Statuscode}", context.TraceIdentifier, nameof(ValidateHandshake), context.Response.StatusCode);
             return false;
         }
 
@@ -193,16 +198,16 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         {
             if (securityProfile == 2 && session.UriScheme == "ws")
             {
+                logger.LogTrace("{id} {func} {securityProfile} auth failed", context.TraceIdentifier, nameof(ValidateHandshake), securityProfile);
                 context.Response.StatusCode = StatusCodes.Status401Unauthorized;
-                logger.LogTrace("{id} {func} {Statuscode}", context.TraceIdentifier, nameof(ValidateHandshake), context.Response.StatusCode);
                 return false;
             }
 
             //if (session.Items.ContainsKey("Authorization") || session.Items.ContainsKey("authorization"))
             if (string.IsNullOrEmpty(authHeader))
             {
+                logger.LogTrace("{id} {func} {securityProfile} auth failed", context.TraceIdentifier, nameof(ValidateHandshake), securityProfile);
                 context.Response.StatusCode = StatusCodes.Status401Unauthorized;
-                logger.LogTrace("{id} {func} {Statuscode}", context.TraceIdentifier, nameof(ValidateHandshake), context.Response.StatusCode);
                 return false;
             }
             //authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
@@ -234,8 +239,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
 
             if (!authorizated)
             {
+                logger.LogTrace("{id} {func} {securityProfile} auth failed", context.TraceIdentifier, nameof(ValidateHandshake), securityProfile);
                 context.Response.StatusCode = StatusCodes.Status401Unauthorized;
-                logger.LogTrace("{id} {func} {Statuscode}", context.TraceIdentifier, nameof(ValidateHandshake), context.Response.StatusCode);
                 return false;
             }
         }