diff --git a/cmd/rdpgw/security/basic.go b/cmd/rdpgw/security/basic.go
index 595fe81cba2b6444ebdfabb4eb6b0e965a9f13c1..e2596b781c0ca4ef530d1a9e6eb3bea6c373b912 100644
--- a/cmd/rdpgw/security/basic.go
+++ b/cmd/rdpgw/security/basic.go
@@ -23,18 +23,18 @@ func CheckHost(ctx context.Context, host string) (bool, error) {
 	case "roundrobin", "unsigned":
 		var username string
 
-		log.Printf("Checking host")
 		s := getSessionInfo(ctx)
-		if s == nil {
+		if s == nil || s.UserName == "" {
 			var ok bool
 			username, ok = ctx.Value("preferred_username").(string)
 			if !ok {
 				return false, errors.New("no valid session info or username found in context")
 			}
 		}
+		log.Printf("Checking host for user %s", username)
 		for _, h := range Hosts {
 			if username != "" {
-				h = strings.Replace(h, "{{ preferred_username }}", s.UserName, 1)
+				h = strings.Replace(h, "{{ preferred_username }}", username, 1)
 			}
 			if h == host {
 				return true, nil