/
AuthentitficationDataService.cs
61 lines (57 loc) · 2.23 KB
/
AuthentitficationDataService.cs
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
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NDG.DataAccessModels;
using System.IO;
using NDG.Common;
namespace NDG.StorageAccess
{
public class AuthentitficationDataService
{
private AuthentificateUserCallback _callback;
public void AuthentificateUser(string userName, string password, string serverUrl, AuthentificateUserCallback callback)
{
_callback = callback;
var request = HttpWebRequest.Create(string.Concat(serverUrl, ServerMethodAddresses.AuthentificateUser));
request.Credentials = new NetworkCredential(userName, password);
var result = request.BeginGetResponse(AuthentificateUserCallback, request);
}
private void AuthentificateUserCallback(IAsyncResult result)
{
try
{
var request = (WebRequest)result.AsyncState;
var response = (WebResponse)request.EndGetResponse(result);
using (var reader = new StreamReader(response.GetResponseStream()))
{
if (reader.ReadToEnd().Equals("OK"))
_callback.Invoke(AuthentificationCode.LoginSuccessed);
else
_callback.Invoke(AuthentificationCode.InvalidCredentials);
}
}
catch (Exception ex)
{
if (ex is WebException)
{
var response = ((HttpWebResponse)((WebException)ex).Response);
if (response.StatusCode == HttpStatusCode.Unauthorized)
_callback.Invoke(AuthentificationCode.InvalidCredentials);
else
_callback.Invoke(AuthentificationCode.ServerNotFound);
}
else if (ex is ArgumentException)
{
_callback.Invoke(AuthentificationCode.ServerNotFound);
}
}
}
}
}