我需要什么身份验证来保护WebAPI 2



我有一个网站,它正在返回1000行,以便在公共前端显示。

我正在使用dataTables(https://datatables.net/examples/data_sources/server_side.html)对于我从C#WebAPI 2中获取一些数据的网站,该网站是由我们自己创建的返回JSON数据。

然后,该表使用其他属性来设置按钮等的样式以及与行/单元格相关的值。

考虑到这是公开的,我想为WebAPI添加某种形式的身份验证。经过研究,大多数人都在使用Identity,但这个项目没有使用Identity。

然后我读了关于JWT(我相信Json Web Token(的文章,但没有任何可供我尝试或断开链接的例子。

我遇到了Base64,但据我所知,它不能与Https协议一起使用,而这个WebAPI最终会走向Https协议。

其他一些文章似乎与有关。我没有使用的Net Core版本。

有没有一种简单的方法来保护这个Web API?如果有,我需要什么类型的身份验证?

因此,需要什么将取决于您的需求。但是,通常对于公开可用的API,使用OAuth2 OpenID Connect。

区分:

  • OpenID connect(位于OAuth2之上(在处理用户时
  • 使用机器对机器(服务对服务(身份验证时的OAuth2

公开可用与公开使用不同。如果你没有真正的客户,或者有一个封闭的网络(通过互联网(,你可以使用相互证书访问之类的。

对于OpenIDc或OAuth,有几个提供商:

  • Identity Server 4(跟踪此服务器的生命周期,去年发生了很多变化(
  • Auth0.一个SaaS解决方案-目前有一个免费层
  • AWS cognito
  • Azure Active Directory B2C
  • 谷歌云当然也有

作为Auth0的一个例子,他们在自己的网站上提供了(一个可能过时的例子(:

示例取自AUTH0,用于上下文-请验证

public void ConfigureServices(IServiceCollection services)
{
// Cookie configuration for HTTP to support cookies with SameSite=None
services.ConfigureSameSiteNoneCookies();
// Cookie configuration for HTTPS
// services.Configure<CookiePolicyOptions>(options =>
// {
//     options.MinimumSameSitePolicy = SameSiteMode.None;
// });
// Add authentication services
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("Auth0", options => {
// Set the authority to your Auth0 domain
options.Authority = $"https://{Configuration["Auth0:Domain"]}";
// Configure the Auth0 Client ID and Client Secret
options.ClientId = Configuration["Auth0:ClientId"];
options.ClientSecret = Configuration["Auth0:ClientSecret"];
// Set response type to code
options.ResponseType = OpenIdConnectResponseType.Code;
// Configure the scope
options.Scope.Clear();
options.Scope.Add("openid");
// Set the callback path, so Auth0 will call back to http://localhost:3000/callback
// Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard
options.CallbackPath = new PathString("/callback");
// Configure the Claims Issuer to be Auth0
options.ClaimsIssuer = "Auth0";
});
// Add framework services.
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

来源:https://auth0.com/docs/quickstart/webapp/aspnet-core-2

最新更新