使用OpenIDConnect回复Azure AD应用程序中的URL

时间:2016-11-28 18:02:49

标签: asp.net azure openid-connect azure-active-directory

我正在使用Azure AD与OpenIdConnect和一个回复URL网站,但我需要通过LocalHost连接进行测试并实现其他功能。

如何使用UseOpenIdConnectAuthentication拥有多个回复网址,并且两者都没有丢失访问权限。

我的应用程序配置了Asp.Net Web.Forms(Visual Studio 2015)。

韩国社交协会

维莱拉

2 个答案:

答案 0 :(得分:1)

是的,可以使用RedirectToIdentityProvider动态更改回复网址。您可以参考下面的代码示例:

app.UseOpenIdConnectAuthentication(
           new OpenIdConnectAuthenticationOptions
           {
               ClientId = clientId,
               Authority = authority,
               PostLogoutRedirectUri = postLogoutRedirectUri,
               RedirectUri = postLogoutRedirectUri,
               Notifications = new OpenIdConnectAuthenticationNotifications
               {
                   AuthenticationFailed = context =>
                   {
                       context.HandleResponse();
                       context.Response.Redirect("/Error?message=" + context.Exception.Message);
                       return Task.FromResult(0);
                   },
                    RedirectToIdentityProvider=(context)=>
                    {
                        context.ProtocolMessage.RedirectUri = "";
                        return Task.FromResult(0);
                    }
               }
           });

但是,如果应用程序已部署到Web服务器,则将重定向URL更改为localhost可能无法正常工作,因为Web应用程序有两个不同的应用程序服务器正在运行。

答案 1 :(得分:1)

是的,它有效,但我需要实现其他代码,例如:

RedirectToIdentityProvider = (context) =>
                    {
                        // This ensures that the address used for sign in and sign out is picked up dynamically from the request
                        // this allows you to deploy your app (to Azure Web Sites, for example)without having to change settings
                        // Remember that the base URL of the address used here must be provisioned in Azure AD beforehand.
                        string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
                        context.ProtocolMessage.RedirectUri = appBaseUrl;
                        context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;
                        return System.Threading.Tasks.Task.FromResult(0);
                    },

但是,我对多租户有问题。其他用户在我的租户中进行身份验证。这是我的问题还是Azure问题?

韩国社交协会, 维莱拉