diff --git a/ConsoleRemoteWebApiCall/CallApiFromConsole/App.config b/ConsoleRemoteWebApiCall/CallApiFromConsole/App.config index 75d0347a..aea2a710 100644 --- a/ConsoleRemoteWebApiCall/CallApiFromConsole/App.config +++ b/ConsoleRemoteWebApiCall/CallApiFromConsole/App.config @@ -1,13 +1,13 @@ - + - + - - + + diff --git a/ConsoleRemoteWebApiCall/CallApiFromConsole/CallApiFromConsole.csproj b/ConsoleRemoteWebApiCall/CallApiFromConsole/CallApiFromConsole.csproj index bb931c4e..069c3d6c 100644 --- a/ConsoleRemoteWebApiCall/CallApiFromConsole/CallApiFromConsole.csproj +++ b/ConsoleRemoteWebApiCall/CallApiFromConsole/CallApiFromConsole.csproj @@ -9,7 +9,7 @@ Properties CallApiFromConsole CallApiFromConsole - v4.5.2 + v4.6.1 512 true ..\ @@ -37,16 +37,16 @@ - ..\packages\Abp.0.7.8.0\lib\net452\Abp.dll - True + False + ..\..\..\aspnetboilerplate\src\Abp\bin\Debug\Abp.dll - ..\packages\Abp.Web.0.7.8.0\lib\net452\Abp.Web.dll - True + False + ..\..\..\aspnetboilerplate\src\Abp.Web\bin\Debug\Abp.Web.dll - ..\packages\Abp.Web.Api.0.7.8.0\lib\net452\Abp.Web.Api.dll - True + False + ..\..\..\aspnetboilerplate\src\Abp.Web.Api\bin\Debug\Abp.Web.Api.dll ..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll diff --git a/ConsoleRemoteWebApiCall/CallApiFromConsole/Program.cs b/ConsoleRemoteWebApiCall/CallApiFromConsole/Program.cs index 8fb98531..d3b199b3 100644 --- a/ConsoleRemoteWebApiCall/CallApiFromConsole/Program.cs +++ b/ConsoleRemoteWebApiCall/CallApiFromConsole/Program.cs @@ -9,6 +9,7 @@ using Abp.Dependency; using Abp.Domain.Entities.Auditing; using Abp.Extensions; using Abp.IO.Extensions; +using Abp.Json; using Abp.Modules; using Abp.Threading; using Abp.Web.Models; @@ -52,9 +53,19 @@ namespace CallApiFromConsole return; } - Console.WriteLine("Logging in..."); + Console.Write("Cookie based (C) or Token based (T) auth (default: C)?"); + var authType = Console.ReadLine() ?? "C"; - client.Object.Login(); + if (authType.ToUpperInvariant() == "T") + { + Console.WriteLine("Logging in with TOKEN based auth..."); + client.Object.TokenBasedAuth(); + } + else + { + Console.WriteLine("Logging in with COOKIE based auth..."); + client.Object.CookieBasedAuth(); + } Console.WriteLine("Getting roles..."); @@ -108,13 +119,14 @@ namespace CallApiFromConsole _abpWebApiClient = abpWebApiClient; } - public void Login() + public void CookieBasedAuth() { - var cookies = LoginAndGetCookies(BaseUrl + "Account/Login", TenancyName, UserName, Password); - foreach (Cookie cookie in cookies) - { - _abpWebApiClient.Cookies.Add(cookie); - } + CookieBasedAuth(BaseUrl + "Account/Login"); + } + + public void TokenBasedAuth() + { + TokenBasedAuth(BaseUrl + "api/Account/Authenticate"); } public async Task> GetRolesAsync() @@ -124,9 +136,9 @@ namespace CallApiFromConsole ); } - private static CookieCollection LoginAndGetCookies(string url, string tenancyName, string userName, string password) + private void CookieBasedAuth(string url) { - var requestBytes = Encoding.UTF8.GetBytes("TenancyName=" + tenancyName + "&UsernameOrEmailAddress=" + userName + "&Password=" + password); + var requestBytes = Encoding.UTF8.GetBytes("TenancyName=" + TenancyName + "&UsernameOrEmailAddress=" + UserName + "&Password=" + Password); var request = WebRequest.CreateHttp(url); @@ -151,10 +163,67 @@ namespace CallApiFromConsole throw new Exception("Could not login. Reason: " + ajaxResponse.Error.Message + " | " + ajaxResponse.Error.Details); } - return response.Cookies; + _abpWebApiClient.Cookies.Clear(); + foreach (Cookie cookie in response.Cookies) + { + _abpWebApiClient.Cookies.Add(cookie); + } } } } + + private void TokenBasedAuth(string url) + { + var token = AsyncHelper.RunSync(() => + _abpWebApiClient.PostAsync( + url, + new + { + TenancyName = TenancyName, + UsernameOrEmailAddress = UserName, + Password = Password + })); + + _abpWebApiClient.RequestHeaders.Add(new NameValue("Authorization", "Bearer " + token)); + + #region Alternative implementation: Manual HTTP request + + //var requestBytes = Encoding.UTF8.GetBytes((new + //{ + // TenancyName = TenancyName, + // UsernameOrEmailAddress = UserName, + // Password = Password + //}).ToJsonString()); + + //var request = WebRequest.CreateHttp(url); + + //request.Method = WebRequestMethods.Http.Post; + //request.ContentType = "application/json"; + //request.Accept = "application/json"; + //request.ContentLength = requestBytes.Length; + + //using (var stream = request.GetRequestStream()) + //{ + // stream.Write(requestBytes, 0, requestBytes.Length); + // stream.Flush(); + + // using (var response = (HttpWebResponse)request.GetResponse()) + // { + // var responseString = Encoding.UTF8.GetString(response.GetResponseStream().GetAllBytes()); + // var ajaxResponse = JsonString2Object(responseString); + + // if (!ajaxResponse.Success) + // { + // throw new Exception("Could not login. Reason: " + ajaxResponse.Error.Message + " | " + ajaxResponse.Error.Details); + // } + + // var token = ajaxResponse.Result.ToString(); + // _abpWebApiClient.RequestHeaders.Add(new NameValue("Authorization", "Bearer " + token)); + // } + //} + + #endregion + } private static TObj JsonString2Object(string str) {