@@ -16,74 +16,76 @@ public class HostStrategy : IMultiTenantStrategy
16
16
17
17
public HostStrategy ( string template )
18
18
{
19
- // New in 2.1, match whole domain if just "__tenant__".
20
- if ( template == Constants . TenantToken )
19
+ // match whole domain if just "__tenant__".
20
+ if ( template == Constants . TenantToken )
21
+ {
22
+ template = template . Replace ( Constants . TenantToken , "(?<identifier>.+)" ) ;
23
+ }
24
+ else
25
+ {
26
+ // Check for valid template.
27
+ // Template cannot be null or whitespace.
28
+ if ( string . IsNullOrWhiteSpace ( template ) )
29
+ {
30
+ throw new MultiTenantException ( "Template cannot be null or whitespace." ) ;
31
+ }
32
+
33
+ // Wildcard "*" must be only occur once in template.
34
+ if ( Regex . Match ( template , @"\*(?=.*\*)" ) . Success )
35
+ {
36
+ throw new MultiTenantException ( "Wildcard \" *\" must be only occur once in template." ) ;
37
+ }
38
+
39
+ // Wildcard "*" must be only token in template segment.
40
+ if ( Regex . Match ( template , @"\*[^\.]|[^\.]\*" ) . Success )
41
+ {
42
+ throw new MultiTenantException ( "\" *\" wildcard must be only token in template segment." ) ;
43
+ }
44
+
45
+ // Wildcard "?" must be only token in template segment.
46
+ if ( Regex . Match ( template , @"\?[^\.]|[^\.]\?" ) . Success )
21
47
{
22
- template = template . Replace ( Constants . TenantToken , "(?<identifier>.+) ") ;
48
+ throw new MultiTenantException ( " \" ? \" wildcard must be only token in template segment. ") ;
23
49
}
24
- else
50
+
51
+ template = template . Trim ( ) . Replace ( "." , @"\." ) ;
52
+ string wildcardSegmentsPattern = @"(\.[^\.]+)*" ;
53
+ string singleSegmentPattern = @"[^\.]+" ;
54
+ if ( template . Substring ( template . Length - 3 , 3 ) == @"\.*" )
25
55
{
26
- // Check for valid template.
27
- // Template cannot be null or whitespace.
28
- if ( string . IsNullOrWhiteSpace ( template ) )
29
- {
30
- throw new MultiTenantException ( "Template cannot be null or whitespace." ) ;
31
- }
32
- // Wildcard "*" must be only occur once in template.
33
- if ( Regex . Match ( template , @"\*(?=.*\*)" ) . Success )
34
- {
35
- throw new MultiTenantException ( "Wildcard \" *\" must be only occur once in template." ) ;
36
- }
37
- // Wildcard "*" must be only token in template segment.
38
- if ( Regex . Match ( template , @"\*[^\.]|[^\.]\*" ) . Success )
39
- {
40
- throw new MultiTenantException ( "\" *\" wildcard must be only token in template segment." ) ;
41
- }
42
- // Wildcard "?" must be only token in template segment.
43
- if ( Regex . Match ( template , @"\?[^\.]|[^\.]\?" ) . Success )
44
- {
45
- throw new MultiTenantException ( "\" ?\" wildcard must be only token in template segment." ) ;
46
- }
47
-
48
- template = template . Trim ( ) . Replace ( "." , @"\." ) ;
49
- string wildcardSegmentsPattern = @"(\.[^\.]+)*" ;
50
- string singleSegmentPattern = @"[^\.]+" ;
51
- if ( template . Substring ( template . Length - 3 , 3 ) == @"\.*" )
52
- {
53
- template = template . Substring ( 0 , template . Length - 3 ) + wildcardSegmentsPattern ;
54
- }
55
-
56
- wildcardSegmentsPattern = @"([^\.]+\.)*" ;
57
- template = template . Replace ( @"*\." , wildcardSegmentsPattern ) ;
58
- template = template . Replace ( "?" , singleSegmentPattern ) ;
59
- template = template . Replace ( Constants . TenantToken , @"(?<identifier>[^\.]+)" ) ;
56
+ template = template . Substring ( 0 , template . Length - 3 ) + wildcardSegmentsPattern ;
60
57
}
61
58
62
- this . regex = $ "^{ template } $";
59
+ wildcardSegmentsPattern = @"([^\.]+\.)*" ;
60
+ template = template . Replace ( @"*\." , wildcardSegmentsPattern ) ;
61
+ template = template . Replace ( "?" , singleSegmentPattern ) ;
62
+ template = template . Replace ( Constants . TenantToken , @"(?<identifier>[^\.]+)" ) ;
63
63
}
64
64
65
+ this . regex = $ "^{ template } $";
66
+ }
67
+
65
68
public Task < string ? > GetIdentifierAsync ( object context )
66
69
{
67
- if ( ! ( context is HttpContext httpContext ) )
68
- throw new MultiTenantException ( null ,
69
- new ArgumentException ( $ "\" { nameof ( context ) } \" type must be of type HttpContext", nameof ( context ) ) ) ;
70
+ if ( context is not HttpContext httpContext )
71
+ return Task . FromResult < string ? > ( null ) ;
70
72
71
- var host = httpContext . Request . Host ;
73
+ var host = httpContext . Request . Host ;
72
74
73
- if ( host . HasValue == false )
74
- return Task . FromResult < string ? > ( null ) ;
75
+ if ( host . HasValue == false )
76
+ return Task . FromResult < string ? > ( null ) ;
75
77
76
- string ? identifier = null ;
78
+ string ? identifier = null ;
77
79
78
- var match = Regex . Match ( host . Host , regex ,
79
- RegexOptions . ExplicitCapture ,
80
- TimeSpan . FromMilliseconds ( 100 ) ) ;
80
+ var match = Regex . Match ( host . Host , regex ,
81
+ RegexOptions . ExplicitCapture ,
82
+ TimeSpan . FromMilliseconds ( 100 ) ) ;
81
83
82
- if ( match . Success )
83
- {
84
- identifier = match . Groups [ "identifier" ] . Value ;
85
- }
86
-
87
- return Task . FromResult ( identifier ) ;
84
+ if ( match . Success )
85
+ {
86
+ identifier = match . Groups [ "identifier" ] . Value ;
88
87
}
88
+
89
+ return Task . FromResult ( identifier ) ;
90
+ }
89
91
}
0 commit comments