如何解决需要 Azure b2c 授权的 Dotnet 核心 webapi 发送错误,Android Volley 无法使用
我正在使用 Volley 连接到受 Azure B2C 保护的 dot net core web api。问题是我从 API 返回了一个 HTML 响应,这使得在客户端应用程序中很难识别和解决。
我得到的回复是:
背后的HTML:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><title>Logging in...</title><Meta name='CACHE-CONTROL' content='NO-CACHE'/><Meta name='PRAGMA' content='NO-CACHE'/><Meta name='EXPIRES' content='-1'/></head><body><form id='auto' method='post' action='https://localhost:5001/signin-oidc'><div><input type='hidden' name='error' id='error' value='redirect_uri_mismatch'/><input type='hidden' name='error_description' id='error_description' value='AADB2C90006: The redirect URI 'https://dev.webapi.enablewear.com/signin-oidc' provided in the request is not registered for the client id '[guid here]'.
Correlation ID: [guid here]
Timestamp: 2021-02-19 13:28:34Z
'/><input type='hidden' name='state' id='state' value='[token here]'/></div><div id='noJavascript' style='visibility: visible; font-family: Verdana'><p>Although we have detected that you have Javascript disabled,you will be able to use the site as normal.</p><p>As part of the authentication process this page may be displayed several times. Please use the continue button below.</p><input type='submit' value='Continue' /></div><script type='text/javascript'>
<!--
document.getElementById('noJavascript').innerHTML = ''; document.getElementById('auto').submit();
//--></script></form></body></html>
我可以通过 Volley StringRequest 获取该信息的唯一方法:
StringRequest stringRequest = new StringRequest(Request.Method.POST,url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG,"onResponse: " + response);
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,"onErrorResponse: " + error.toString());
}
}) {
@Override
protected Map<String,String> getParams() throws AuthFailureError {
HashMap<String,String> param = new HashMap<>();
//param.put(IndicationDM.NAME,indication.getName());
return param;
}
@Override
public Map<String,String> getHeaders() throws AuthFailureError {
HashMap<String,String> header = new HashMap<>();
Log.d(TAG,"getHeaders: Bearer " + accesstoken);
header.put("Authorization","Bearer " + accesstoken);
return header;
}
};
我宁愿使用 JsonObjectRequest 并处理这样的错误:
if ( error instanceof AuthFailureError ) {
Log.d(TAG,"onError: AuthFailureError");
} else if (error instanceof NetworkError) {
Log.d(TAG,"onError: NetworkError");
} else if (error instanceof ParseError) {
Log.d(TAG,"onError: ParseError");
}
但问题是没有调用 AuthFailureError,ParseError 是因为上面的 HTML 不是 JSON。我希望结果作为 json 返回或启动 AuthFailureError,但我不确定是否有需要更改的 webapi 配置或 Azure b2c 配置。
dotnet 核心 webapi 控制器正在使用 [Authorize] 属性。我可能需要刷新令牌,但我会遇到这个问题并想要捕获/响应它。
有什么想法吗?
更新
我查看了 this example 并对 startup.cs 进行了更改,现在出现了 AuthFailureError,这很好。我仍然发现诊断挑战非常困难,这是另一篇文章的主题。这是我更改的内容:
致:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAdB2C",options);
options.TokenValidationParameters.NameClaimType = "name";
},options => { Configuration.Bind("AzureAdB2C",options); });
services.AddControllers();
services.AddAuthorization();
发件人:
services.AddMicrosoftIdentityWebAppAuthentication(Configuration,"AzureAdB2C");
我不能肯定地说我没有改变任何其他东西。我试图对服务器进行调整,但这实际上是我记得所做/测试的最后一次更改。
解决方法
我假设这里的“受 B2C 保护”意味着它被部署到 Azure Web App service using Easy Auth 仍然是这种情况。一旦您将应用服务配置为需要身份验证,它将期望在授权标头中获得访问令牌或在请求中获得授权 cookie。应用服务不区分托管 API 或 Web 应用 (html)。如果它没有获得访问令牌或 cookie,它将假定浏览器正在尝试访问 Web 应用程序并启动 OIDC 重定向到服务器。调用 API 的客户端应将 302 响应视为未经授权。您的 Web Api 不应使用 [Authorize] 或尝试验证传入的令牌。 Azure 服务会做到这一点。您可以根据文章使用环境变量或 ClaimsPrincipal 检索应用代码中的声明(和令牌)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。