Added support for .net 4.6

This commit is contained in:
Jonas Dellinger 2018-09-04 14:39:07 +02:00
parent f97fd83cca
commit 2f8bf6877f
9 changed files with 70 additions and 31 deletions

View File

@ -7,6 +7,7 @@ namespace SpotifyAPI.Web.Auth
{
public static void OpenBrowser(string url)
{
#if NETSTANDARD2_0
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
@ -24,6 +25,10 @@ namespace SpotifyAPI.Web.Auth
{
// throw
}
#else
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
#endif
}
}
}

View File

@ -1,10 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SpotifyAPI.Web.Enums;
@ -12,6 +9,13 @@ using SpotifyAPI.Web.Models;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;
using Unosquare.Labs.EmbedIO.Modules;
#if NETSTANDARD2_0
using System.Net.Http;
#endif
#if NET46
using System.Net.Http;
using HttpListenerContext = Unosquare.Net.HttpListenerContext;
#endif
namespace SpotifyAPI.Web.Auth
{
@ -91,7 +95,11 @@ namespace SpotifyAPI.Web.Auth
Error = error
}));
#if NETSTANDARD2_0
return context.StringResponseAsync("OK - This window can be closed now");
#else
return context.StringResponseAsync("OK - This window can be closed now");
#endif
}
[WebApiHandler(HttpVerbs.Post, "/")]
@ -107,9 +115,7 @@ namespace SpotifyAPI.Web.Auth
auth.SecretId = (string) formParams["secretId"];
string uri = auth.GetUri();
context.Response.Redirect(uri);
return true;
return context.Redirect(uri, false);
}
}
}
}

View File

@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Threading.Tasks;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
@ -18,20 +19,22 @@ namespace SpotifyAPI.Web.Auth
protected override WebServer AdaptWebServer(WebServer webServer)
{
return webServer.WithWebApiController<ImplictGrantAuthController>();
Console.WriteLine("Hello");
webServer.Module<WebApiModule>().RegisterController<ImplictGrantAuthController>();
return webServer;
}
}
public class ImplictGrantAuthController : WebApiController
{
[WebApiHandler(HttpVerbs.Get, "/auth")]
public Task<bool> GetAuth(WebServer server, HttpListenerContext context)
[WebApiHandler(HttpVerbs.Get, "/authe")]
public bool GetAuth(WebServer server, HttpListenerContext context)
{
string state = context.Request.QueryString["state"];
SpotifyAuthServer<Token> auth = ImplictGrantAuth.GetByState(state);
if (auth == null)
return context.StringResponseAsync(
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");
return true; /*context.StringResponseAsync(
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");*/
Token token;
string error = context.Request.QueryString["error"];
@ -56,7 +59,8 @@ namespace SpotifyAPI.Web.Auth
}
Task.Factory.StartNew(() => auth?.TriggerAuth(token));
return context.StringResponseAsync("OK - This window can be closed now");
return true;
// return context.StringResponseAsync("OK - This window can be closed now");
}
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EmbedIO" Version="1.15.0" />
<PackageReference Include="EmbedIO" Version="1.16.0" />
</ItemGroup>
<ItemGroup>
@ -30,4 +30,9 @@
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>

View File

@ -23,7 +23,9 @@
</dependencies>
</metadata>
<files>
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\SpotifyAPI.Web.Auth.dll" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\SpotifyAPI.Web.Auth.xml" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.dll" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.xml" />
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.dll" target="lib\net46\SpotifyAPI.Web.Auth.dll" />
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.xml" target="lib\net46\SpotifyAPI.Web.Auth.xml" />
</files>
</package>

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading;
using SpotifyAPI.Web.Enums;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;
using Unosquare.Labs.EmbedIO.Modules;
namespace SpotifyAPI.Web.Auth
@ -20,6 +21,7 @@ namespace SpotifyAPI.Web.Auth
private readonly string _folder;
private readonly string _type;
private WebServer _server;
protected CancellationTokenSource _serverSource;
public delegate void OnAuthReceived(object sender, T payload);
@ -41,10 +43,13 @@ namespace SpotifyAPI.Web.Auth
{
Instances.Add(State, this);
_serverSource = new CancellationTokenSource();
WebServer server = AdaptWebServer(WebServer.Create(ServerUri));
server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
_server = WebServer.Create(ServerUri, RoutingStrategy.Regex);
_server.RegisterModule(new WebApiModule());
AdaptWebServer(_server);
_server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
#pragma warning disable 4014
server.RunAsync(_serverSource.Token);
_server.RunAsync(_serverSource.Token);
#pragma warning restore 4014
}

View File

@ -27,14 +27,19 @@ namespace SpotifyAPI.Web.Example
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
AuthorizationCodeAuth auth =
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
auth.AuthReceived += AuthOnAuthReceived;
AuthorizationCodeAuth auth =
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
auth.AuthReceived += AuthOnAuthReceived;
auth.Start();
auth.OpenBrowser();
/* ImplictGrantAuth auth = new ImplictGrantAuth("26d287105e31491889f3cd293d85bfea", "http://localhost:4002", "http://localhost:4002");
auth.Start();
auth.OpenBrowser();
auth.OpenBrowser();*/
Console.ReadLine();
auth.Stop(0);
}
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -18,4 +18,9 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>

View File

@ -22,7 +22,9 @@
</dependencies>
</metadata>
<files>
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\SpotifyAPI.Web.dll" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\SpotifyAPI.Web.xml" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\netstandard2.0\SpotifyAPI.Web.dll" />
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\netstandard2.0\SpotifyAPI.Web.xml" />
<file src="bin\Release\net46\SpotifyAPI.Web.dll" target="lib\net46\SpotifyAPI.Web.dll" />
<file src="bin\Release\net46\SpotifyAPI.Web.xml" target="lib\net46\SpotifyAPI.Web.xml" />
</files>
</package>