Added alternative token based authentication for console remote api demo.
This commit is contained in:
Родитель
58f5c5628c
Коммит
a0b339a610
|
@ -1,13 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
|
||||
</startup>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
|
||||
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>CallApiFromConsole</RootNamespace>
|
||||
<AssemblyName>CallApiFromConsole</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
|
||||
|
@ -37,16 +37,16 @@
|
|||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Abp, Version=0.7.8.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Abp.0.7.8.0\lib\net452\Abp.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\aspnetboilerplate\src\Abp\bin\Debug\Abp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Abp.Web, Version=0.7.8.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Abp.Web.0.7.8.0\lib\net452\Abp.Web.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\aspnetboilerplate\src\Abp.Web\bin\Debug\Abp.Web.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Abp.Web.Api, Version=0.7.8.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Abp.Web.Api.0.7.8.0\lib\net452\Abp.Web.Api.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\aspnetboilerplate\src\Abp.Web.Api\bin\Debug\Abp.Web.Api.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
|
||||
|
|
|
@ -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<ListResultOutput<RoleListDto>> 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<string>(
|
||||
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<AjaxResponse>(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<TObj>(string str)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче