c# - Xamarin.Essentials WebAuthenticator 服务器端实现重定向到登录页面而不是 401?
问题描述
我正在尝试理解 WebAuthenticator 文档和示例。我的目标是在应用程序(oauth 2)中有一个基于服务器的社交登录。
在使用这个实现的时候,必须假设我在另一个Controller中可以使用该[Authorize]
属性来要求认证。但是当我这样做时,它会尝试重定向到 /Account/Login ,而 /Account/Login 又不存在,给出 404。
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Sample.Server.WebAuthenticator.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class SampleController : ControllerBase
{
// GET: api/Sample
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
因此,从 Android 模拟器中尝试这个概念会导致 api 控制器 url “未找到”,即 404,而不是像 401 那样需要某种身份验证。
要对此进行测试,请从上面下载 Xamarin Essentials 示例。使用 API 读/写模板添加 SampleController。构建 Sample.Server.WebAuthenticator,将其设置为唯一启动项目并进行调试。确保您可以访问 /api/sample 并接收 json 结果。接下来将 [Authorize] 添加到 SampleController,并对其进行调试。它将重定向到丢失的登录页面。
我有点困惑,服务器示例不像 Wildermuth 的Jwt 文章那样工作
通知应用程序应该进行身份验证的正确方法是什么?
解决方案
路由到 /signin-microsoft 时,在示例服务器中验证 Microsoft 帐户时返回以下错误:
例外:OAuth 令牌端点失败:状态:BadRequest;标头:缓存控制:无存储,无缓存
推荐阅读
- r - 多项式回归方程
- google-cloud-firestore - 我可以查询集合组中的子集合吗?
- c# - 将“视野”转换为“正交尺寸”
- java - Lambda 表达式 - 使用 lambda 表达式
- django - 如何向 Browsable API Root 添加视图?
- python - 当对象为非整数时 sp.delete() 不起作用
- go - Golang chan & waitgroup 有时要等很久?
- python - 使用 Python API 下载数据时如何消除(或从控制台抑制)Interactive Brokers 错误
- ios - 如何在 UITextView Swift 4 中使某些单词可点击以进行类似模式弹出的操作?
- python - 如何创建将重复值映射到多个键和清理数据的字典?