Authentication 为什么';我的cookie身份验证在asp.NETCore中不起作用吗?
我已启用使用Cookie的身份验证:Authentication 为什么';我的cookie身份验证在asp.NETCore中不起作用吗?,authentication,asp.net-core,Authentication,Asp.net Core,我已启用使用Cookie的身份验证: 使用Microsoft.AspNetCore.Builder; 使用Microsoft.Extensions.DependencyInjection; 使用Microsoft.Extensions.Logging; 使用WebBasic.Core.Controllers; 名称空间WebBasic.Core { 公营创业 { public void配置服务(IServiceCollection服务) { services.AddMvc(); services
使用Microsoft.AspNetCore.Builder;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用WebBasic.Core.Controllers;
名称空间WebBasic.Core
{
公营创业
{
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddAuthentication().AddCookie(AuthController.AuthControllerAuthId);
services.AddAuthorization(options=>{options.AddPolicy(“IsFoo”,policy=>policy.requirecall(“FooType”);});
}
public void配置(IApplicationBuilder应用程序、ILoggerFactory loggerfactory)
{
app.UseMvc();
app.UseAuthentication();
}
}
}
…并创建了一个通用视图来测试声明:
使用System.Collections.Generic;
使用System.Linq;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Authentication;
使用Microsoft.AspNetCore.Mvc;
命名空间WebBasic.Core.Controllers
{
[路由(“/api/[controller]/[action]”)
公共类AuthController:Controller
{
公用常量字符串AuthControllerAuthId=“AuthControllerAuthId”;
公共IActionResult测试()
{
var claims=string.Join(“,”,User.claims.Select(i=>i.Type));
返回Ok($“{User.Identity.IsAuthenticated}:{claims}”);
}
公共异步任务AuthFixed()
{
var claims=new List{new claims(“FooType”,“FooValue”,ClaimValueTypes.String)};
var userIdentity=新的索赔实体(索赔);
var userPrincipal=newclaimsprincipal(userIdentity);
等待HttpContext.SignInAsync(AuthControllerAuthId,userPrincipal);
返回Ok(“Ok”);
}
公共异步任务注销()
{
等待HttpContext.SignOutAsync(AuthControllerAuthId);
返回Ok(“Ok”);
}
}
}
当我得到一个cookie集时:
Cache-Control:no-cache
Content-Type:text/plain; charset=utf-8
Date:Tue, 05 Sep 2017 13:57:39 GMT
Expires:-1
Pragma:no-cache
Server:Kestrel
Set-Cookie:.AspNetCore.AuthControllerAuthId=...; path=/; samesite=lax; httponly
…但当我返回到api/auth/test
时,我得到:
False
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
我没有经过认证?身份验证中间件没有接收我的cookie?为什么不呢
如果我添加[Authorize]
我会得到:
False
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
System.InvalidOperationException:未指定authenticationScheme,也未找到DefaultChallengeScheme。
在Microsoft.AspNetCore.Authentication.AuthenticationService.d_u11.MoveNext()中
???为什么我需要显式地设置它
我知道,我可以将DefaultChallengeScene、DefaultAuthenticationScheme和DefaultScheme设置为CookieAuthenticationDefaults.AuthenticationScheme
,但实际上所做的只是设置为字符串“Cookies”,它仍然不起作用
这里到底发生了什么?对于其他有此问题的人,诀窍是所有组件都需要引用相同的标识id 在我的例子中,我使用了
AuthControllerAuthId
作为我的自定义身份验证标识符,而不是默认的Cookie
。我需要的是确保auth链的所有部分都使用这个值
添加身份验证时:
services.AddAuthentication(选项=>
{
options.DefaultChallengeScheme=AuthController.AuthControllerAuthId;
options.DefaultAuthenticateScheme=AuthController.AuthControllerAuthId;
options.DefaultScheme=AuthController.AuthControllerAuthId;
}).AddCookie(AuthController.AuthControllerAuthId)
构建索赔实体时
:
var userIdentity=newclaimsidentity(声明、AuthControllerAuthId);
var userPrincipal=newclaimsprincipal(userIdentity);
登录时:
wait-HttpContext.SignInAsync(AuthControllerAuthId,userPrincipal);
实际上一切都正常,但在我的例子中,诀窍是查看
User.Identity
上的AuthenticationType
,并确保在所有步骤中都正确设置了它。您是否尝试放置app.UseAuthentication()代码>在app.UseMvc()之前代码>在配置方法中?