EasyApiProxy.HawkAuth
4.0.0
dotnet add package EasyApiProxy.HawkAuth --version 4.0.0
NuGet\Install-Package EasyApiProxy.HawkAuth -Version 4.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyApiProxy.HawkAuth" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyApiProxy.HawkAuth --version 4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasyApiProxy.HawkAuth, 4.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EasyApiProxy.HawkAuth as a Cake Addin #addin nuget:?package=EasyApiProxy.HawkAuth&version=4.0.0 // Install EasyApiProxy.HawkAuth as a Cake Tool #tool nuget:?package=EasyApiProxy.HawkAuth&version=4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasyApiProxy
- 簡單的 Web Api 代理類別產生
- 支援Hawk驗證 與 BearerToken驗證
- Nuget Package Install
- 提供 DefaultApi協定 實作支援 於套件 EasyApiProxy.WebApi
- 提供 用戶端 Hawk驗證 於套件 EasyApiProxy.HawkAuth
呼叫範例
// 建立API 代理物件 Factory 必須重複使用 因為其實做包含一個專用的 HttpClient
// HttpClient 微軟官方明確的說必須公用 否則回有tcp/ip資源不足的問題
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
.Build<IDemoApi>();
// 建立代理物件
using (var proxy = factory.Create()) {
var api = proxy.Api;
// 呼叫Api
var ret1 = await api.Login(new Login { Account = "david", Password = "123" });
// 傳遞Bearer Token 後續呼叫會自動帶上
proxy.SetBearer(ret1.Token);
}
Api 介面定義
// 展示Api 定義
// 注意 DefaultApi協定 輸入參數只能0或1個。
public interface IDemoApi
{
Task<AccountInfo> Login(Login req);
Task Logout(TokenInfo req);
Task<string> GetEmail(TokenInfo req);
string GetServerInfo();
}
後台實作範例 Microsoft.AspNet.WebApi.Owin
- 參考套件 EasyApiProxy.WebApi 提供 DefaultApiResult 實作
/// <summary>
/// 範例API
/// </summary>
[DefaultApiResult] // 套用DefaultApi 通訊封裝
public partial class DemoApiController : ApiController, IDemoApi
{
[HttpPost]
public async Task<AccountInfo> Login(Login req)
{
await Task.Delay(1000);
if (req.Account == "david" && req.Password == "123")
{
return new AccountInfo { Account = req.Account, Token = "123456789", Expired = DateTime.Now.AddHours(1) };
}
throw new NotImplementedException();
}
[HttpPost]
public async Task Logout(TokenInfo req)
{
await Task.Delay(1000);
if (req.Token == "123456789")
return;
throw new ApplicationException("The Token Not exists");
}
[HttpPost]
public async Task<string> GetEmail(TokenInfo req)
{
await Task.Delay(1000);
if (req.Token == "123456789")
{
return "david@gmail.com";
}
throw new ApplicationException("The Token Not exists");
}
[HttpPost]
public string GetServerInfo()
{
return "Demo Server";
}
}
// Startup 啟動規劃WebApi
public class Startup
{
public void Configuration(IAppBuilder app) {
var apiConfig = new System.Web.Http.HttpConfiguration();
// 啟用WebApi
{
// use attibute routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// 必須套用 DefaultApi Json 設定
config.Formatters.JsonFormatter.SerializerSettings = DefaultApiExtension.DefaultJsonSerializerSettings;
//use webapi
app.UseWebApi(apiConfig);
}
}
}
用戶端啟用HAWK驗證
- 參考套件 EasyApiProxy.HawkAuth
// hawk 證書
var credential = new HawkNet.HawkCredential();
credential.Id = "API";
credential.Key = "XXXXXXXX";
credential.Algorithm = "sha256";
// proxy factory
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
// 啟用Hawk驗證
.UseHawkAuthorize(credential)
.Build<IDemoApi>();
// 建立代理物件
var proxy = factory.Create();
var api = proxy.Api;
// 呼叫Api
var ret = await api.Login(new Login { Account = "david", Password = "123" });
後台API端啟用HAWK驗證 範例 Microsoft.AspNet.WebApi.Owin
- 引用套件 HawkNet.Owin
// Startup 啟動規劃WebApi 與 Hawk驗證
public class Startup
{
public void Configuration(IAppBuilder app) {
var apiConfig = new System.Web.Http.HttpConfiguration();
// 啟用Hawk驗證
{
var credential = new HawkCredential();
credential.Id = "API";
credential.Key = "XXXXXXXX";
credential.User = "API";
credential.Algorithm = "sha256";
app.UseHawkAuthentication(new HawkAuthenticationOptions
{
Credentials = (id) => Task.FromResult(credential),
IncludeServerAuthorization = false,
TimeskewInSeconds = 120
});
}
// 啟用WebApi
{
// use attibute routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// 必須套用 DefaultApi Json 設定
config.Formatters.JsonFormatter.SerializerSettings = DefaultApiExtension.DefaultJsonSerializerSettings;
//use webapi
app.UseWebApi(apiConfig);
}
}
}
/// <summary>
/// 範例API
/// </summary>
[DefaultApiResult] //套用DefaultApi 回傳格式
[Authorize(Users="API")] // 要求授權通過
public partial class DemoApiController : ApiController, IDemoApi {
...
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net451 is compatible. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.5.1
- EasyApiProxy (>= 4.0.0)
- HawkNet (>= 1.4.4)
- Newtonsoft.Json (>= 12.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.