Skip to content

Commit 91e01a3

Browse files
committed
Merge pull request #6 from Unity3dAzure/multiplatform
Windows and Android ServerCertificateValidation update.
2 parents c89c7c2 + 2d4e340 commit 91e01a3

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

Assets/MobileServices/client/MobileServiceClient.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
using RestSharp;
44
using System.Collections.Generic;
55
using System;
6+
#if !NETFX_CORE || UNITY_ANDROID
7+
using System.Net;
8+
using System.Security.Cryptography.X509Certificates;
9+
using System.Net.Security;
10+
#endif
611

712
namespace Unity3dAzure.MobileServices
813
{
@@ -22,6 +27,12 @@ public MobileServiceClient(string appUrl, string appKey) : base(appUrl)
2227
{
2328
AppUrl = appUrl;
2429
AppKey = appKey;
30+
31+
// required for running in Windows and Android
32+
#if !NETFX_CORE || UNITY_ANDROID
33+
Debug.Log("ServerCertificateValidation");
34+
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
35+
#endif
2536
}
2637

2738
public override string ToString()
@@ -41,7 +52,7 @@ public void Login(MobileServiceAuthenticationProvider provider, string token, Ac
4152
{
4253
string uri = "login/" + provider.ToString().ToLower();
4354
ZumoRequest request = new ZumoRequest(this, uri, Method.POST);
44-
Debug.Log( "Login Request Uri: " + uri );
55+
Debug.Log("Login Request Uri: " + uri + " access token: " + token);
4556
request.AddBodyAccessToken(token);
4657
this.ExecuteAsync(request, callback);
4758
}
@@ -67,5 +78,20 @@ public void Login(MobileServiceAuthenticationProvider provider)
6778
this.ExecuteAsync(request, callback);
6879
}
6980

81+
#if !NETFX_CORE || UNITY_ANDROID
82+
private bool RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
83+
{
84+
// Check the certificate to see if it was issued from Azure
85+
if (certificate.Subject.Contains("azurewebsites.net"))
86+
{
87+
return true;
88+
}
89+
else
90+
{
91+
return false;
92+
}
93+
}
94+
#endif
95+
7096
}
7197
}
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using UnityEngine;
22
using System.Collections;
33
using RestSharp;
4-
#if !NETFX_CORE
5-
using System.Net;
6-
using System.Security.Cryptography.X509Certificates;
7-
using System.Net.Security;
8-
#endif
94

105
namespace Unity3dAzure.MobileServices
116
{
@@ -22,10 +17,6 @@ public ZumoRequest(MobileServiceClient client, string uri, Method httpMethod) :
2217
this.AddHeader("X-ZUMO-AUTH", client.User.authenticationToken);
2318
Debug.Log("Auth UserId:" + client.User.user.userId);
2419
}
25-
// required for running in Windows
26-
#if !NETFX_CORE
27-
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
28-
#endif
2920
}
3021

3122
public void AddBodyAccessToken(string token)
@@ -34,20 +25,5 @@ public void AddBodyAccessToken(string token)
3425
this.AddBody(accessToken);
3526
}
3627

37-
#if !NETFX_CORE
38-
private bool RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
39-
{
40-
// Check the certificate to see if it was issued from Azure
41-
if ( certificate.Subject.Contains("azurewebsites.net") )
42-
{
43-
return true;
44-
}
45-
else
46-
{
47-
return false;
48-
}
49-
}
50-
#endif
51-
5228
}
5329
}

Assets/MobileServices/model/AccessToken.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ public AccessToken(string accessTokenValue)
1111
{
1212
access_token = accessTokenValue;
1313
}
14+
15+
/// Needed only for Serialization (Fixes error: AccessToken cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute)
16+
public AccessToken() {}
1417
}
1518
}

Assets/Scenes/HighscoresDemo.unity

288 Bytes
Binary file not shown.

Assets/Scripts/HighscoresDemoUI.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ public class HighscoresDemoUI : MonoBehaviour
3737

3838
/// Use this for initialization
3939
void Start ()
40-
{
41-
/// NB: Warning this block disables ServerCertificateValidation on Android for demo purposes only!
42-
#if UNITY_ANDROID
43-
Debug.Log("Warning: Android ServerCertificateValidation disabled.");
44-
ServicePointManager.ServerCertificateValidationCallback = (p1, p2, p3, p4) => true; // NB: this is a workaround for "Unable to find /System/Library/Frameworks/Security.framework/Security" issue in Android
45-
#endif
46-
40+
{
4741
/// Create Mobile Service client
4842
_client = new MobileServiceClient(_appUrl, _appKey);
4943
Debug.Log(_client);
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)