diff --git a/SpotifyAPI.Web.Auth/AuthUtil.cs b/SpotifyAPI.Web.Auth/AuthUtil.cs index a6ff6545..d6e40e45 100644 --- a/SpotifyAPI.Web.Auth/AuthUtil.cs +++ b/SpotifyAPI.Web.Auth/AuthUtil.cs @@ -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 } } } \ No newline at end of file diff --git a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs index beaec0cb..e9662868 100644 --- a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs +++ b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs @@ -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); } } -} \ No newline at end of file +} diff --git a/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs b/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs index 80477126..8cdf7a79 100644 --- a/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs +++ b/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs @@ -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(); + Console.WriteLine("Hello"); + webServer.Module().RegisterController(); + return webServer; } } public class ImplictGrantAuthController : WebApiController { - [WebApiHandler(HttpVerbs.Get, "/auth")] - public Task GetAuth(WebServer server, HttpListenerContext context) + [WebApiHandler(HttpVerbs.Get, "/authe")] + public bool GetAuth(WebServer server, HttpListenerContext context) { string state = context.Request.QueryString["state"]; SpotifyAuthServer 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"); } } -} \ No newline at end of file +} diff --git a/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.csproj b/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.csproj index 6dde69c6..4d7343e5 100644 --- a/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.csproj +++ b/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard2.0;net46 @@ -15,7 +15,7 @@ - + @@ -30,4 +30,9 @@ + + + + + diff --git a/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.nuspec b/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.nuspec index b0c630b4..11cdbe9a 100644 --- a/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.nuspec +++ b/SpotifyAPI.Web.Auth/SpotifyAPI.Web.Auth.nuspec @@ -23,7 +23,9 @@ - - + + + + diff --git a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs index 9b7d6199..81eca9e3 100644 --- a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs +++ b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs @@ -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 } diff --git a/SpotifyAPI.Web.Example/Program.cs b/SpotifyAPI.Web.Example/Program.cs index ece744d9..0aa144e4 100644 --- a/SpotifyAPI.Web.Example/Program.cs +++ b/SpotifyAPI.Web.Example/Program.cs @@ -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) diff --git a/SpotifyAPI.Web/SpotifyAPI.Web.csproj b/SpotifyAPI.Web/SpotifyAPI.Web.csproj index ab6966be..038dde9b 100644 --- a/SpotifyAPI.Web/SpotifyAPI.Web.csproj +++ b/SpotifyAPI.Web/SpotifyAPI.Web.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard2.0;net46 @@ -18,4 +18,9 @@ + + + + + diff --git a/SpotifyAPI.Web/SpotifyAPI.Web.nuspec b/SpotifyAPI.Web/SpotifyAPI.Web.nuspec index 9891c6df..9ceee6e5 100644 --- a/SpotifyAPI.Web/SpotifyAPI.Web.nuspec +++ b/SpotifyAPI.Web/SpotifyAPI.Web.nuspec @@ -22,7 +22,9 @@ - - + + + +