【问题标题】:Azure Active Directory website redirect unexpectedAzure Active Directory 网站重定向意外
【发布时间】:2017-01-01 09:10:05
【问题描述】:

我在 Azure Active Directory 网站配置回复 URL 中指定了两个 URL。一个在我运行本地代码时重定向到我的 localhost 环境,一个在我运行 prod 网站时重定向到我的 Azure 托管网站。但 Azure Active Directory 似乎忽略了该设置。它只使用一个或另一个 URL,但不能同时使用两者 这是我的startup.Auth.cs

public partial class Startup
{
    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
    private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

    private static string authority = aadInstance + tenantId;    

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.GivenName;

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,                    

                TokenValidationParameters = new TokenValidationParameters
                {                                                
                    RoleClaimType = "roles"
                },

            });            
    }
}

这是我的 startup.cs

 public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);      
    }

最后这是我的 web.config 设置

<appSettings>
<add key="ida:ClientId" value="*************************" />
<add key="ida:AADInstance" value="https://login.microsoftonline.com/" />
<add key="ida:AppKey" value="******************************" />
<add key="ida:TenantId" value="****************************" />
<add key="ida:PostLogoutRedirectUri" value="url of production website" />
<add key="ida:Domain" value="company domain" />
</appSettings>

我不知道为什么会发生这种重定向

【问题讨论】:

    标签: c# azure owin azure-active-directory


    【解决方案1】:

    我找到了解决问题的方法

    您应该在 Startup.Auth 的 OpenIdConnectAuthenticationOptions 中添加以下代码

                   Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        RedirectToIdentityProvider = (context) =>
                        {
                            context.ProtocolMessage.RedirectUri = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Path);
                            context.ProtocolMessage.PostLogoutRedirectUri = new UrlHelper(HttpContext.Current.Request.RequestContext).Action("Index", "Home", null, HttpContext.Current.Request.Url.Scheme);
                            context.ProtocolMessage.Resource = GraphAPIIdentifier;                            
                            return Task.FromResult(0);
                        }}
    

    这种方式重定向uri是根据你运行它的机器动态的

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2019-03-03
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多