Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

IsAuthenticated is false when moved to the IIS server with Azure AD .Net Core 2.1 #8700

Closed
sandillio opened this issue Nov 8, 2018 · 18 comments

Comments

@sandillio
Copy link

Is this a Bug or Feature request?:

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

Description of the problem:

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

@sandillio
Copy link
Author

sandillio commented Nov 8, 2018

Works perfectly on my local machine but when moved to server it returns false.

My Configure Services method looks like this

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
            
        });

        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
        })
            .AddWsFederation(WsFederationDefaults.AuthenticationScheme, options =>
            {
                options.MetadataAddress = "MetaDataAddress";
                options.Wtrealm = "WtRealm";
                options.Wreply = "https://mydomain/AzureADDemo/Home/Status";
                options.SaveTokens = true;
                

            }).AddCookie(
                options =>
                {

                    options.Cookie.Name = ".AspNet.SharedCookie";
                    options.LoginPath = "/AzureADDemo/Home/Signin";
                    options.Cookie.Path = "/AzureADDemo";
                    options.Cookie.Expiration = TimeSpan.FromMinutes(20); 
                    options.Cookie.SecurePolicy = CookieSecurePolicy.None;
                    options.Cookie.SameSite = SameSiteMode.None;
                });

        services.AddMvc();

       
    }

And my Configure Method looks like this

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
        app.UseCors(policy => policy.SetIsOriginAllowed(origin => origin == 
                     "https://login.microsoftonline.com"));
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        

        app.UseCookiePolicy();
        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

@sandillio sandillio changed the title IsAuthenticated is false when moved to the IIS server IsAuthenticated is false when moved to the IIS server with Azure AD .Net Core 2.1 Nov 8, 2018
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @sandillio.
@javiercn, can you please look into this? Thanks!

@javiercn
Copy link
Member

javiercn commented Nov 8, 2018

@Tratcher I believe this is all yours

@javiercn javiercn assigned Tratcher and unassigned javiercn Nov 8, 2018
@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

Can you share a Fiddler trace of the scenario?

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

Where are you calling IsAuthenticated?

@sandillio
Copy link
Author

sandillio commented Nov 8, 2018

@Tratcher Here is the Fillder Trace
image

@sandillio
Copy link
Author

sandillio commented Nov 8, 2018

@Tratcher This is how I am signing in
var redirectUrl = Url.Action(nameof(HomeController.Status), "Home");
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, WsFederationDefaults.AuthenticationScheme);
And then in Status I am checking for IsAuthenticated which coming as False

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

Please upload the fiddler trace file rather than a screenshot. You can send it to the e-mail in my profile if you don't want it to be public.

I expect your problem is with the Wreply option, that shouldn't point to a page in your app, but to a url handled directly by the middleware.. Use CallbackPath instead of wreply.

@mkArtakMSFT feel free to transfer this to the Security repo.

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

Confirmed, your Wreply is wrong. Use CallbackPath instead as described in the doc above.

@sandillio
Copy link
Author

sandillio commented Nov 8, 2018

@Tratcher So I have removed Wreply and added callback path but now I am getting the error saying the reply URL is not matching.

.AddWsFederation(WsFederationDefaults.AuthenticationScheme, options =>
{
options.MetadataAddress = "MetaDataAddress";
options.Wtrealm = "WtRealm";
options.CallbackPath = "/Home/Status";
options.SaveTokens = true;
}

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

CallbackPath should not reference a page in your app, it should be a dedicated endpoint like the default value "/signin-wsfed". The auth middleware will handle requests to this path.

@sandillio
Copy link
Author

@Tratcher So I have changed my CallBackPath = "/AzureADDemo", my Reply Urls in AzureAD as "https://mydomain/AzureADDemo" since this is the endpoint referring to Wreply and I still get reply url not matching error. Please correct me as I am going wrong and having difficulty understanding this ADD.

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

and I still get reply url not matching error.

What's the exact error and where do you get it? That doesn't sound like an ASP.NET error, is it coming from AAD?

@sandillio
Copy link
Author

sandillio commented Nov 8, 2018 via email

@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2018

You're going to need to work that out with AAD. The only advice I can give is that you need to be very careful of the value, AAD has been known to require exact matches, even case sensitive.

@mkArtakMSFT
Copy link
Member

Thanks @Tratcher.

Closing this as there is no more action to be taken here from our side.

@sandillio
Copy link
Author

sandillio commented Nov 14, 2018

@Tratcher Sorry for posting late, but got it working. my solution is, I change the reply url in Azure to be as http://mydomain/yourappname/signin-wsfed and my configureServices method to be as below.

public void ConfigureServices(IServiceCollection services)
{

    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;            
    });

    services.AddAuthentication(sharedOptions =>
    {
        sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
    }) .AddWsFederation(WsFederationDefaults.AuthenticationScheme, options =>
        {
            options.MetadataAddress = "MetaDataAddress";
            options.Wtrealm = "WtRealm";
            options.SaveTokens = true;
            

        }).AddCookie(
            options =>
            {

                options.Cookie.Name = ".AspNet.SharedCookie";
                options.LoginPath = "/signin-wsfed";
                options.Cookie.Expiration = TimeSpan.FromMinutes(20); 
                options.Cookie.SecurePolicy = CookieSecurePolicy.None;
                options.Cookie.SameSite = SameSiteMode.None;
            });

    services.AddMvc();       
}

@Tratcher
Copy link
Member

Remove options.LoginPath = "/signin-wsfed";, it doesn't belong there. LoginPath won't be used unless someone calls Challenge on Cookies rather than WsFed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants