[英]RestSharp with JWT-authentication doesn't work
这是我“学习”如何做的页面: https : //stormpath.com/blog/token-authentication-asp-net-core
但是对我来说这不起作用(也不适用于Fiddler)我的ApplicationUser-model有这个控制器:
[Authorize] //works when it's not set, doesn't work when it's set
[Route("api/[controller]")]
public class ApplicationUserController : Controller
{
private IRepository<ApplicationUser> _applicationUserRepository;
public ApplicationUserController(IRepository<ApplicationUser> applicationUserRepository)
{
_applicationUserRepository = applicationUserRepository;
}
[HttpGet("{id}")]
public ApplicationUser Get(int id)
{
return _applicationUserRepository.Get(id);
}
}
我的包装是RestSharp的,以获取所有应用程序用户:
public Task<T> GetResponseContentAsync<T>(string resource, int id) where T : new()
{
RestRequest request = new RestRequest($"{resource}/{{id}}", Method.GET);
request.AddUrlSegment("id", id);
if (!AuthenticationToken.IsNullOrEmpty(true))
{
request.AddHeader("Authorization", string.Format("Bearer {0}", AuthenticationToken));
_client.Authenticator = new JwtAuthenticator(AuthenticationToken);
_client.Authenticator.Authenticate(_client, request);
}
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
_client.ExecuteAsync<T>(request, response =>
{
tcs.SetResult(response.Data);
});
return tcs.Task;
}
我想从Web客户端应用程序中使用JWT(令牌身份验证)登录。 登录后,我得到这个如的access_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJURVNUIiwianRpIjoiZTBjYjE0NjgtYzBmOS00ZTM4LTg4ZjgtMGM4ZjNmYjMyNjZmIiwiaWF0IjoxNDcwOTUwMTA0LCJuYmYiOjE0NzA5NTAxMDQsImV4cCI6MTQ3MDk1MDQwNCwiaXNzIjoiRXhhbXBsZUlzc3VlciIsImF1ZCI6IkV4YW1wbGVBdWRpZW5jZSJ9.a9_JK2SG3vzc6NSOB0mZXqHlM9UAEXUHHrrijAQUsX0
没有Authorize
-attribute,我得到了ApplicationUser,但是在设置Attribute时,结果为null(因为未调用web-api)
包装器调用如下所示:
//this works, token-value is set
string token = new RepositoryCall("http://localhost:54008/").Login("token", "TEST", "TEST123");
string accessToken = JsonConvert.DeserializeObject<Dictionary<string, string>>(token)["access_token"];
ViewData["Result"] = accessToken;
ApplicationUser userAfterLogin = await new RepositoryCall("http://localhost:54008/api")
{ AuthenticationToken = accessToken }
.GetResponseContentAsync<ApplicationUser>("ApplicationUser", 2);
此处userAfterLogin
为null。
自两个星期以来,我一直在尝试获取登录信息,但仍然无法正确登录。
知道我在做什么错吗? 也许是错误的授权请求标头值?
更新资料
这是我的Startup.Configure,配置为使用Bearer / JWT:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
var secretKey = "mysupersecret_secretkey!123";
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
var options = new TokenProviderOptions
{
Audience = "ExampleAudience",
Issuer = "ExampleIssuer",
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
};
var tokenValidationParameters = new TokenValidationParameters
{
// The signing key must match!
ValidateIssuerSigningKey = true,
IssuerSigningKey = signingKey,
// Validate the JWT Issuer (iss) claim
ValidateIssuer = true,
ValidIssuer = "ExampleIssuer",
// Validate the JWT Audience (aud) claim
ValidateAudience = true,
ValidAudience = "ExampleAudience",
// Validate the token expiry
ValidateLifetime = true,
// If you want to allow a certain amount of clock drift, set that here:
ClockSkew = TimeSpan.Zero
};
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationScheme = "Cookie",
CookieName = "access_token",
TicketDataFormat = new CustomJwtDataFormat(
SecurityAlgorithms.HmacSha256,
tokenValidationParameters)
});
app.UseMiddleware<TokenProviderMiddleware>(Options.Create(options));
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
如果您收到授权错误或使用邮递员,您将意识到您被要求重定向到登录名,只需使用以下命令装饰您的班级:
[Authorize(AuthenticationSchemes = "Bearer")]
默认情况下,.Net使用基于cookie的身份验证,带有该注释的您将粘贴到基于令牌的令牌中
因此,您正在使用2个中间件进行身份验证。 一个由asp.net身份提供(基于cookie),另一个由令牌提供。 现在,两个中间件都使用相同的属性来处理请求[授权]。 更精确地看一下这里的代码
对于JWTBearer
和
饼干
因为两者都在中间件管道中激活,所以当您发送身份验证令牌或cookie时,主体将拥有数据。
但是因为它们都处于活动状态,所以对于没有cookie或JwtBearer的请求,它们中的任何一个都将返回Unauthorized。
对于您要寻找的解决方案,您需要在现有的cookie和令牌的基础上创建一个中间件,以根据是否存在授权标头将请求路由到其中一个。
在Fiddler中,您将看到是否将您重定向到登录页面(它将报告2个结果,其中一个为302(重定向),然后为404)-是这样吗?
您已激活DebugLogger,因此请尝试AddDebug(LogLevel.Trace)并查看“调试”输出窗口,这对于分析哪些身份验证步骤失败非常有帮助。 它还显示身份验证是否失败或授权,以及是否具有有效的令牌等。因此,它指出了查找问题的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.