[4962] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Services.Authentication;
|
---|
| 6 | using System.ServiceModel.Security;
|
---|
| 7 | using System.ServiceModel;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.Services.Authentication.ServiceClients
|
---|
| 10 | {
|
---|
| 11 | public class AuthenticationClient
|
---|
| 12 | {
|
---|
| 13 |
|
---|
| 14 | private static AuthenticationClient instance;
|
---|
| 15 | public static AuthenticationClient Instance
|
---|
| 16 | {
|
---|
| 17 | get
|
---|
| 18 | {
|
---|
| 19 | if (instance == null) instance = new AuthenticationClient();
|
---|
| 20 | return instance;
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | #region Properties
|
---|
| 25 |
|
---|
| 26 | private IEnumerable<User> users;
|
---|
| 27 | public IEnumerable<User> Users
|
---|
| 28 | {
|
---|
| 29 | get { return users; }
|
---|
| 30 | }
|
---|
| 31 | private IEnumerable<Application> applications;
|
---|
| 32 | public IEnumerable<Application> Applications
|
---|
| 33 | {
|
---|
| 34 | get { return applications; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private IEnumerable<Role> roles;
|
---|
| 38 | public IEnumerable<Role> Roles
|
---|
| 39 | {
|
---|
| 40 | get { return roles; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | #endregion
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | private AuthenticationClient()
|
---|
| 47 | {
|
---|
| 48 | applications = CallService<Application[]>(s => s.GetApplications()).OrderBy(x => x.Name);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | #region Store
|
---|
| 54 |
|
---|
| 55 | public bool Store(AuthItem item)
|
---|
| 56 | {
|
---|
| 57 | try
|
---|
| 58 | {
|
---|
| 59 | if (item.Id == Guid.Empty)
|
---|
| 60 | {
|
---|
| 61 | if (item is Role)
|
---|
| 62 | item.Id = CallService<Guid>(s => s.InsertRole((Role)item));
|
---|
| 63 | else if (item is User)
|
---|
| 64 | item.Id = CallService<Guid>(s => s.InsertUser((User)item));
|
---|
| 65 | else if (item is Application)
|
---|
| 66 | item.Id = CallService<Guid>(s => s.InsertApplication((Application)item));
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | if (item is Role)
|
---|
| 72 | CallService(s => s.UpdateRole((Role)item));
|
---|
| 73 | else if (item is User)
|
---|
| 74 | CallService(s => s.UpdateUser((User)item));
|
---|
| 75 | else if (item is Application)
|
---|
| 76 | CallService(s => s.UpdateApplication((Application)item));
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 | return true;
|
---|
| 80 | }
|
---|
| 81 | catch (Exception ex)
|
---|
| 82 | {
|
---|
| 83 | //ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
| 84 | return false;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
| 90 | #region Refresh
|
---|
| 91 |
|
---|
| 92 | public void Refresh()
|
---|
| 93 | {
|
---|
| 94 |
|
---|
| 95 | var call = new Func<Exception>(delegate()
|
---|
| 96 | {
|
---|
| 97 |
|
---|
| 98 | Guid applicationId = Guid.Empty; // to be set!
|
---|
| 99 |
|
---|
| 100 | try
|
---|
| 101 | {
|
---|
| 102 | users = CallService<User[]>(s => s.GetUsers(applicationId)).OrderBy(x => x.Name);
|
---|
| 103 | roles = CallService<Role[]>(s => s.GetRoles(applicationId)).OrderBy(x => x.Name);
|
---|
| 104 |
|
---|
| 105 | // ...
|
---|
| 106 |
|
---|
| 107 | return null;
|
---|
| 108 | }
|
---|
| 109 | catch (Exception ex)
|
---|
| 110 | {
|
---|
| 111 | return ex;
|
---|
| 112 | }
|
---|
| 113 | });
|
---|
| 114 | call.BeginInvoke(delegate(IAsyncResult result)
|
---|
| 115 | {
|
---|
| 116 | Exception ex = call.EndInvoke(result);
|
---|
| 117 | if (ex != null)
|
---|
| 118 | {
|
---|
| 119 | //ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | }, null);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | #endregion
|
---|
| 127 |
|
---|
| 128 | private T CallService<T>(Func<IAuthenticationService, T> call)
|
---|
| 129 | {
|
---|
| 130 | AuthenticationServiceClient client = new AuthenticationServiceClient();
|
---|
| 131 | client.ClientCredentials.UserName.UserName = "Alice";
|
---|
| 132 | client.ClientCredentials.UserName.Password = "YouWillKnow";
|
---|
| 133 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
| 134 | //AuthenticationServiceClient client = ClientFactory.Create<AuthenticationClient, IAuthenticationService>();
|
---|
| 135 | try
|
---|
| 136 | {
|
---|
| 137 | return call(client);
|
---|
| 138 | }
|
---|
| 139 | finally
|
---|
| 140 | {
|
---|
| 141 | try
|
---|
| 142 | {
|
---|
| 143 | client.Close();
|
---|
| 144 | }
|
---|
| 145 | catch (Exception)
|
---|
| 146 | {
|
---|
| 147 | client.Abort();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | #region Application methods
|
---|
| 154 |
|
---|
| 155 | public Application GetApplication(Guid applicationId)
|
---|
| 156 | {
|
---|
| 157 | try
|
---|
| 158 | {
|
---|
| 159 | return CallService<Application>(s => s.GetApplication(applicationId));
|
---|
| 160 | }
|
---|
| 161 | catch (Exception ex)
|
---|
| 162 | {
|
---|
| 163 | //ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
|
---|
| 164 | return null;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | #endregion
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | //public static void Main() {
|
---|
| 173 | // AuthenticationServiceClient auth = new AuthenticationServiceClient();
|
---|
| 174 |
|
---|
| 175 | // auth.ClientCredentials.UserName.UserName = "Alice";
|
---|
| 176 | // auth.ClientCredentials.UserName.Password = "YouWillKnow";
|
---|
| 177 | // auth.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
| 178 |
|
---|
| 179 | // Application[] apps = auth.GetApplications();
|
---|
| 180 | // foreach (Application app in apps) {
|
---|
| 181 | // Console.WriteLine(app.Name);
|
---|
| 182 | // }
|
---|
| 183 | // Console.ReadLine();
|
---|
| 184 | //}
|
---|
| 185 | }
|
---|
| 186 | }
|
---|