asp.net-mvc - OpenIdConnect 是唯一在 Azure Active Directory 中实现授权的中间件

标签 asp.net-mvc azure openid owin ws-federation

我们正在尝试使用 Azure Active Directory 为我们的网站设置身份验证和授权。

我们首先使用 WS-Federation OWIN 中间件进行身份验证,效果非常好(根据 this sample )。

然后,我们尝试插入授权并陷入困境 - Azure Active Directory 中基于角色和组的设置都需要如下内容:

AuthorizationCodeReceived = context =>
                    {
                        // Get Access Token for User's Directory
                        string userObjectId = context.AuthenticationTicket.Identity.FindFirst(Globals.ObjectIdClaimType).Value;
                        string tenantId = context.AuthenticationTicket.Identity.FindFirst(Globals.TenantIdClaimType).Value;
                        ClientCredential credential = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.AppKey);
                        AuthenticationContext authContext = new AuthenticationContext(
                            String.Format(CultureInfo.InvariantCulture, ConfigHelper.AadInstance, tenantId),
                            new TokenDbCache(userObjectId));
                        AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                            context.Code,
                            new Uri(
                                string.Format(
                                    URI_MANGLER, 
                                    HttpContext.Current.Request.Url.Scheme, 
                                    HttpContext.Current.Request.Url.Authority, 
                                    System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)),
                            credential, 
                            ConfigHelper.GraphResourceId);

                        return Task.FromResult(0);
                    }

使用 WS-Federation,上下文中不存在“代码”字段之类的内容。

在 AAD 中设置租户,添加应用程序并更新 list 以具有多个角色。用户被分配到应用程序并被赋予特定的角色。

因此,我们将所有内容都移至 OpenId,但问题是:这是处理此类要求的最愚蠢的方法吗?

最佳答案

即使使用 WS-Federation,您也绝对可以获得角色并针对 [Authorize] 使用它们。只需按照 https://github.com/AzureADSamples/WebApp-RoleClaims-DotNet 自述文件中的说明进行操作即可用于在 azure AD 中打开应用程序的角色创建和发射。此外,您必须确保类型 http://schemas.microsoft.com/ws/2008/06/identity/claims/role 的声明用作应用程序中的角色类型。无论如何,这应该是默认值。

关于asp.net-mvc - OpenIdConnect 是唯一在 Azure Active Directory 中实现授权的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074573/

相关文章:

.net - 从 .net 应用程序登录到 OpenID 站点

asp.net-mvc - Select2 自动完成搜索不显示数据 asp.net mvc 5

javascript - flow.js 点击上传文件

azure - 如何访问本地 PC 上的模拟 Azure 存储

c# - Azure 计划 Web 作业设置

asp.net-mvc - Azure OpenID 通过 OWIN 中间件连接导致无限重定向循环

asp.net-mvc - 如何使用 Autofac 将 "bind"属性添加到操作过滤器?

asp.net-mvc - MVC 架构 - 重新使用相同的 View 模型进行读取和编辑

azure - 将此消息发送到您的机器人时出错 : HTTP status code NotFound error in test in web chat

oauth-2.0 - 谷歌 OpenID 连接 : How to verify id_token?