Skip to content

Commit 2f8bf68

Browse files
committed
Added support for .net 4.6
1 parent f97fd83 commit 2f8bf68

File tree

9 files changed

+70
-31
lines changed

9 files changed

+70
-31
lines changed

SpotifyAPI.Web.Auth/AuthUtil.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal static class AuthUtil
77
{
88
public static void OpenBrowser(string url)
99
{
10+
#if NETSTANDARD2_0
1011
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1112
{
1213
url = url.Replace("&", "^&");
@@ -24,6 +25,10 @@ public static void OpenBrowser(string url)
2425
{
2526
// throw
2627
}
28+
#else
29+
url = url.Replace("&", "^&");
30+
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
31+
#endif
2732
}
2833
}
2934
}

SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Net;
5-
using System.Net.Http;
64
using System.Text;
7-
using System.Threading;
85
using System.Threading.Tasks;
96
using Newtonsoft.Json;
107
using SpotifyAPI.Web.Enums;
118
using SpotifyAPI.Web.Models;
129
using Unosquare.Labs.EmbedIO;
1310
using Unosquare.Labs.EmbedIO.Constants;
1411
using Unosquare.Labs.EmbedIO.Modules;
12+
#if NETSTANDARD2_0
13+
using System.Net.Http;
14+
#endif
15+
#if NET46
16+
using System.Net.Http;
17+
using HttpListenerContext = Unosquare.Net.HttpListenerContext;
18+
#endif
1519

1620
namespace SpotifyAPI.Web.Auth
1721
{
@@ -91,7 +95,11 @@ public Task<bool> GetEmpty(WebServer server, HttpListenerContext context)
9195
Error = error
9296
}));
9397

98+
#if NETSTANDARD2_0
9499
return context.StringResponseAsync("OK - This window can be closed now");
100+
#else
101+
return context.StringResponseAsync("OK - This window can be closed now");
102+
#endif
95103
}
96104

97105
[WebApiHandler(HttpVerbs.Post, "/")]
@@ -107,9 +115,7 @@ public bool PostValues(WebServer server, HttpListenerContext context)
107115
auth.SecretId = (string) formParams["secretId"];
108116

109117
string uri = auth.GetUri();
110-
context.Response.Redirect(uri);
111-
112-
return true;
118+
return context.Redirect(uri, false);
113119
}
114120
}
115-
}
121+
}

SpotifyAPI.Web.Auth/ImplictGrantAuth.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
23
using System.Threading.Tasks;
34
using SpotifyAPI.Web.Enums;
45
using SpotifyAPI.Web.Models;
@@ -18,20 +19,22 @@ public ImplictGrantAuth(string clientId, string redirectUri, string serverUri, S
1819

1920
protected override WebServer AdaptWebServer(WebServer webServer)
2021
{
21-
return webServer.WithWebApiController<ImplictGrantAuthController>();
22+
Console.WriteLine("Hello");
23+
webServer.Module<WebApiModule>().RegisterController<ImplictGrantAuthController>();
24+
return webServer;
2225
}
2326
}
2427

2528
public class ImplictGrantAuthController : WebApiController
2629
{
27-
[WebApiHandler(HttpVerbs.Get, "/auth")]
28-
public Task<bool> GetAuth(WebServer server, HttpListenerContext context)
30+
[WebApiHandler(HttpVerbs.Get, "/authe")]
31+
public bool GetAuth(WebServer server, HttpListenerContext context)
2932
{
3033
string state = context.Request.QueryString["state"];
3134
SpotifyAuthServer<Token> auth = ImplictGrantAuth.GetByState(state);
3235
if (auth == null)
33-
return context.StringResponseAsync(
34-
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");
36+
return true; /*context.StringResponseAsync(
37+
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");*/
3538

3639
Token token;
3740
string error = context.Request.QueryString["error"];
@@ -56,7 +59,8 @@ public Task<bool> GetAuth(WebServer server, HttpListenerContext context)
5659
}
5760

5861
Task.Factory.StartNew(() => auth?.TriggerAuth(token));
59-
return context.StringResponseAsync("OK - This window can be closed now");
62+
return true;
63+
// return context.StringResponseAsync("OK - This window can be closed now");
6064
}
6165
}
62-
}
66+
}

SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="EmbedIO" Version="1.15.0" />
18+
<PackageReference Include="EmbedIO" Version="1.16.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>
@@ -30,4 +30,9 @@
3030
<Folder Include="Properties\" />
3131
</ItemGroup>
3232

33+
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
34+
<Reference Include="System.Net" />
35+
<Reference Include="System.Net.Http" />
36+
</ItemGroup>
37+
3338
</Project>

SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.nuspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
</dependencies>
2424
</metadata>
2525
<files>
26-
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\SpotifyAPI.Web.Auth.dll" />
27-
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\SpotifyAPI.Web.Auth.xml" />
26+
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.dll" />
27+
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.xml" />
28+
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.dll" target="lib\net46\SpotifyAPI.Web.Auth.dll" />
29+
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.xml" target="lib\net46\SpotifyAPI.Web.Auth.xml" />
2830
</files>
2931
</package>

SpotifyAPI.Web.Auth/SpotifyAuthServer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using SpotifyAPI.Web.Enums;
77
using Unosquare.Labs.EmbedIO;
8+
using Unosquare.Labs.EmbedIO.Constants;
89
using Unosquare.Labs.EmbedIO.Modules;
910

1011
namespace SpotifyAPI.Web.Auth
@@ -20,6 +21,7 @@ public abstract class SpotifyAuthServer<T>
2021

2122
private readonly string _folder;
2223
private readonly string _type;
24+
private WebServer _server;
2325
protected CancellationTokenSource _serverSource;
2426

2527
public delegate void OnAuthReceived(object sender, T payload);
@@ -41,10 +43,13 @@ public void Start()
4143
{
4244
Instances.Add(State, this);
4345
_serverSource = new CancellationTokenSource();
44-
WebServer server = AdaptWebServer(WebServer.Create(ServerUri));
45-
server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
46+
47+
_server = WebServer.Create(ServerUri, RoutingStrategy.Regex);
48+
_server.RegisterModule(new WebApiModule());
49+
AdaptWebServer(_server);
50+
_server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
4651
#pragma warning disable 4014
47-
server.RunAsync(_serverSource.Token);
52+
_server.RunAsync(_serverSource.Token);
4853
#pragma warning restore 4014
4954
}
5055

SpotifyAPI.Web.Example/Program.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ static void Main(string[] args)
2727
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
2828

2929

30-
AuthorizationCodeAuth auth =
31-
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
32-
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
33-
auth.AuthReceived += AuthOnAuthReceived;
30+
AuthorizationCodeAuth auth =
31+
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
32+
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
33+
auth.AuthReceived += AuthOnAuthReceived;
34+
auth.Start();
35+
auth.OpenBrowser();
36+
37+
/* ImplictGrantAuth auth = new ImplictGrantAuth("26d287105e31491889f3cd293d85bfea", "http://localhost:4002", "http://localhost:4002");
3438
auth.Start();
35-
auth.OpenBrowser();
39+
auth.OpenBrowser();*/
3640

3741
Console.ReadLine();
42+
auth.Stop(0);
3843
}
3944

4045
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)

SpotifyAPI.Web/SpotifyAPI.Web.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -18,4 +18,9 @@
1818
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1919
</ItemGroup>
2020

21+
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
22+
<Reference Include="System.Net" />
23+
<Reference Include="System.Net.Http" />
24+
</ItemGroup>
25+
2126
</Project>

SpotifyAPI.Web/SpotifyAPI.Web.nuspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
</dependencies>
2323
</metadata>
2424
<files>
25-
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\SpotifyAPI.Web.dll" />
26-
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\SpotifyAPI.Web.xml" />
25+
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\netstandard2.0\SpotifyAPI.Web.dll" />
26+
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\netstandard2.0\SpotifyAPI.Web.xml" />
27+
<file src="bin\Release\net46\SpotifyAPI.Web.dll" target="lib\net46\SpotifyAPI.Web.dll" />
28+
<file src="bin\Release\net46\SpotifyAPI.Web.xml" target="lib\net46\SpotifyAPI.Web.xml" />
2729
</files>
2830
</package>

0 commit comments

Comments
 (0)