Moustafa
Moustafa

Reputation: 145

Azure Active Directory website redirect unexpected

I have specified two URLs in my Azure Active Directory website configuration Reply URL. One to redirect to my localhost environment when I am running local code and one to redirect to my Azure hosted website when I am running the prod website. But Azure Active directory seems to be ignoring the setting. It only uses one or the other URL but not both this is my 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"
                },

            });            
    }
}

and this my startup.cs

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

and finally this is my web.config setting

<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>

i dont know why this redirect occur

Upvotes: 0

Views: 601

Answers (1)

Moustafa
Moustafa

Reputation: 145

I found the solution to my problem

you should add the following code in OpenIdConnectAuthenticationOptions in Startup.Auth

               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);
                    }}

This way redirect uri is dynamically based on the machine you run it

Hope this be helpful.

Upvotes: 1

Related Questions