相关疑难解决方法(0)

ASP.NET Core 2.0身份验证中间件

随着Core 1.1遵循@ blowdart的建议并实施了自定义中间件:

/sf/answers/2202565921/

它的工作方式如下:

  1. 中间件运行了.从请求标头中选取一个令牌.
  2. 验证了令牌,如果有效,则构建一个包含多个声明的身份(ClaimsIdentity),然后通过HttpContext.User.AddIdentity()添加;
  3. 在使用services.AddAuthorization的ConfigureServices中,我添加了一个策略来要求中间件提供的声明.
  4. 在控制器/操作中,我将使用[授权(角色="中间件添加的某些角色")]

这有点适用于2.0,除了如果令牌无效(上面的步骤2)并且从未添加声明我得到"没有指定authenticationScheme,并且没有找到DefaultChallengeScheme."

所以现在我正在读取2.0中的auth更改:

https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

在ASP.NET Core 2.0中我做同样的事情的正确途径是什么?我没有看到做真正的自定义身份验证的示例.

c# authentication asp.net-core asp.net-core-2.0

84
推荐指数
2
解决办法
5万
查看次数

使用AspNet Core 2.0进行Google JWT身份验证

我试图在我的ASP.NET Core 2.0 web api中集成google身份验证,我无法弄清楚如何让它工作.

我在我的Startup.cs中有这个代码ConfigureServices:

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders();

services.AddAuthentication()
.AddGoogle(googleOptions => 
 {
     googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
     googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
Run Code Online (Sandbox Code Playgroud)

这个在Configure(IApplicationBuilder app, IHostingEnvironment env):

 app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)

当我导航到Authorized端点时,结果是302 Found因为它可能是重定向到某个登录端点(我从未创建过).如何阻止重定向,只是让API期望一个令牌,401如果没有提供令牌,则返回一个令牌?

google-api google-authentication oauth-2.0 asp.net-core asp.net-core-2.0

5
推荐指数
2
解决办法
5314
查看次数