首页 > 解决方案 > 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 文章那样工作

通知应用程序应该进行身份验证的正确方法是什么?

标签: c#asp.net-corexamarinoauth-2.0xamarin.essentials

解决方案


路由到 /signin-microsoft 时,在示例服务器中验证 Microsoft 帐户时返回以下错误:

例外:OAuth 令牌端点失败:状态:BadRequest;标头:缓存控制:无存储,无缓存


推荐阅读