forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
217 lines (171 loc) · 6.13 KB
/
Copy pathProgram.cs
File metadata and controls
217 lines (171 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading;
using Funq;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Configuration;
using ServiceStack.Web;
namespace CheckHttpListener
{
[DataContract]
public class CustomUserSession : AuthUserSession
{
private int organisationId_;
[DataMember]
public int UtcOffset { get; set; }
[DataMember]
public Int32 CreatedDateUnix { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens,
Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
CreatedDate = DateTime.Now;
CreatedDateUnix = (Int32)(CreatedDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
authService.SaveSession(this);
}
}
public class CustomBasicAuthProvider : BasicAuthProvider
{
public CustomBasicAuthProvider() {}
public CustomBasicAuthProvider(IAppSettings appSettings)
: base(appSettings) {}
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
{
return base.Authenticate(authService, session, request);
}
/// <summary>
/// This method will only send the WWW-Authenticate response header when the User-Agent request
/// header contains servicestack i.e. it's the servicestack client and not a browser
/// </summary>
public override void OnFailedAuthentication(IAuthSession session, IRequest httpReq, IResponse httpRes)
{
//Only return digest header if User-Agent is the ServiceStack .NET Client
//expected user agent value is: "ServiceStack .NET Client {Version}"
if (httpReq.Headers["User-Agent"].ToLower().Contains("servicestack"))
{
httpRes.AddHeader("WWW-Authenticate",
"{0} realm=\"{1}\"".Fmt(Provider, AuthRealm));
}
httpRes.StatusCode = 401;
httpRes.EndRequest(false);
}
}
public static class AppHostConfiguration
{
public static void Configure(ServiceStackHost appHost, Container container)
{
var appSettings = new AppSettings();
var auth = new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new CustomBasicAuthProvider(),
new CredentialsAuthProvider(appSettings) {SessionExpiry = TimeSpan.FromMinutes(30)}
},
"/login")
{
GenerateNewSessionCookiesOnAuthentication = false,
};
appHost.Plugins.Add(auth);
IUserAuthRepository authRepository = new InMemoryAuthRepository();
ICacheClient cacheClient = new MemoryCacheClient();
//IoC registrations
container.Register(cacheClient);
container.Register(authRepository);
var hostConfig = new HostConfig
{
#if DEBUG || STAGING || UAT
DebugMode = true,
#endif
AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Csv },
};
appHost.SetConfig(hostConfig);
}
public static void Start() {}
public static void Stop(bool immediate) {}
}
[Route("/test2")]
public class TestNoAuthRequest : IReturn<string>
{
}
[Route("/test")]
public class TestRequest : IReturn<string>
{
}
public class TestService : Service
{
private CustomUserSession session_;
protected CustomUserSession Session
{
get
{
return session_ ?? (session_ = SessionAs<CustomUserSession>());
}
}
[Authenticate]
public string Any(TestRequest request)
{
return Session.UserAuthId;
}
public string Any(TestNoAuthRequest request)
{
return "Hi";
}
}
public class AppSelfHost : AppSelfHostBase
{
public AppSelfHost()
: base("DocuRec Services", typeof(TestService).Assembly)
{ }
public override void Configure(Container container)
{
AppHostConfiguration.Configure(this, container);
}
/// <summary>
/// Starts the ServiceStackHost and Schedules jobs (if it's the first time being called)
/// </summary>
public override ServiceStackHost Start(string urlBase)
{
return Start(new List<string> { urlBase });
}
/// <summary>
/// Starts the ServiceStackHost and Schedules jobs (if it's the first time being called)
/// </summary>
public override ServiceStackHost Start(IEnumerable<string> urlBases)
{
AppHostConfiguration.Start();
return base.Start(urlBases);
}
public override void Stop()
{
AppHostConfiguration.Stop(true);
base.Stop();
}
}
internal class Program
{
private static void Main(string[] args)
{
var appHost = new AppSelfHost();
appHost.Init();
appHost.Start("http://127.0.0.1:1234/");
Thread.Sleep(2500);
var client = new JsonServiceClient("http://127.0.0.1:1234/")
{
AlwaysSendBasicAuthHeader = true,
Password = "a"
};
var post1 = client.Post(new TestRequest());
var post2 = client.Post(new TestRequest());
var response = "First response: {0}, Second Response: {1}".Fmt(post1, post2);
Console.Out.WriteLine(response);
Console.Read();
}
}
}