-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionHelper.cs
92 lines (77 loc) · 3.08 KB
/
ConnectionHelper.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSDNVideo.Tienda.MSDNVideoServices;
using System.ServiceModel;
using MSDNVideo.Comun;
using System.ServiceModel.Description;
namespace MSDNVideo.Tienda
{
/// <summary>
/// Clase auxiliar para acceder a los servicios web de la aplicación
/// </summary>
class ConnectionHelper
{
private static IMSDNVideoServices _serviceClient;
static ConnectionHelper()
{
}
public static bool SetConnectionData(string usuario, string clave, string address, out string errorMessage)
{
ChannelFactory<IMSDNVideoServices> factory = new ChannelFactory<IMSDNVideoServices>("ws");
IMSDNVideoServices serviceClient;
Usuario usuarioEntity = null;
errorMessage = null;
factory.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(address), factory.Endpoint.Address.Identity, factory.Endpoint.Address.Headers);
factory.Credentials.UserName.UserName = "invitado";
factory.Credentials.UserName.Password = "";
SetDataContractSerializerBehavior(factory.Endpoint.Contract);
serviceClient = factory.CreateChannel();
try
{
usuarioEntity = serviceClient.Usuarios_ValidarUsuario(usuario, clave);
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
if (usuarioEntity == null)
{
errorMessage = "El usuario o la clave son incorrectos";
return false;
}
if (usuarioEntity.Rol != Rol.Admin)
{
errorMessage = "El usuario suministrado no es administrador";
return false;
}
factory = new ChannelFactory<IMSDNVideoServices>("ws");
factory.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(address), factory.Endpoint.Address.Identity, factory.Endpoint.Address.Headers);
factory.Credentials.UserName.UserName = usuario;
factory.Credentials.UserName.Password = clave;
SetDataContractSerializerBehavior(factory.Endpoint.Contract);
_serviceClient = factory.CreateChannel();
return true;
}
private static void SetDataContractSerializerBehavior(ContractDescription contractDescription)
{
foreach (OperationDescription operation in contractDescription.Operations)
{
for (int i = 0; i < operation.Behaviors.Count; i++)
{
if (operation.Behaviors[i] is DataContractSerializerOperationBehavior)
operation.Behaviors[i] = new ReferencePreservingDataContractSerializerOperationBehavior(operation);
}
}
}
public static IMSDNVideoServices ServiceClient
{
get
{
return _serviceClient;
}
}
}
}