【问题标题】:MicrosoftGraphAuthProvider does not respect [Authenticate("microsoftgraph")] attributeMicrosoftGraphAuthProvider 不尊重 [Authenticate("microsoftgraph")] 属性
【发布时间】:2021-11-12 08:17:28
【问题描述】:

我们已经实现了 MicrosoftGraphAuthProvider 并且所有设置都正确,因为我们添加了一个端点来使用以下内容输出授权用户的凭据:

 if (!IsAuthenticated) return null;
 var session = this.Request.GetSession(true);
 return session.ToJson();

这将输出我的用户,提供者为 microsoftgraph。太好了,一切都符合预期。

但是,当我们添加授权属性时:

 [Authenticate("microsoftgraph")]

它返回 401 并表现得好像我们根本没有登录一样。所有 ss-id 和 ss-pid 都在标头中正确发送,但仍返回 401。

但是,在系统的其他地方,我们使用相同的方法来限制 API 密钥身份验证

  [Authenticate("apikey")]

我们目前在 API 中加载了 3 个 IAuthProvider。

提供者本身是否存在问题,或者是否存在将服务限制为 microsfoftgraph 提供者的不同方法?

【问题讨论】:

    标签: microsoft-graph-api authorization servicestack


    【解决方案1】:

    当您使用 Auth Provider 名称时,例如:

    [Authenticate("microsoftgraph")]
    

    它告诉 ServiceStack 与注册的 AuthProvider 进行检查,以确定它是否认为用户已通过调用其 IsAuthorized() 方法进行身份验证,MicrosoftGraphAuthProvider 没有定义,因此它使用其基本 OAuthProvider 实现:

    public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
    {
        if (request != null)
        {
            if (!LoginMatchesSession(session, request.UserName)) return false;
        }
    
        return session != null && session.IsAuthenticated && !string.IsNullOrEmpty(tokens?.AccessTokenSecret);
    }
    

    您可以通过覆盖 AuthProvider 并实现 IsAuthorized 或覆盖 Custom UserSession 并覆盖 IsAuthorized(provider) 来覆盖此行为,例如:

    public class MyUserSession : AuthUserSession
    {
        public override bool IsAuthorized(string provider)
        {
            if (provider == MicrosoftGraphAuthProvider.Name)
                return IsAuthenticated && AuthProvider == provider;
            return base.IsAuthorized(provider);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2021-07-07
      • 2013-06-16
      相关资源
      最近更新 更多