HttpContext Authentication in ASP.Net Core 2.0

Hello guys.

I have the problem which I am not able to solve by myself, friends or Google.

I want to authenticate user in my asp.net core 2.0 web application. The problem is that I can’t use UserManager  or  SignInManager,  because I have stored users in different project. I have identity server, user will authenticate via extension grant, and in my application  I am getting **ClientPrincipal (**There is information about user, and IsAuthenticated property is also true):

**var principal = await GetPrincipalAsync(tokenResponse.AccessToken, model.UserId);**

After getting principal I try to sign in with HttpContext:

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

And here is how my startup class looks:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<AuthClient>();
            services.AddScoped<TPayApiClient>();
            services.AddMvc();
            services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
            services.AddScoped<IUrlHelper>(factory =>
            {
                var actionContext = factory.GetService<IActionContextAccessor>()
                                               .ActionContext;
                return new UrlHelper(actionContext);
            });

 **services.AddAuthentication(options =\> { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", options =\> { options.SignInScheme = "Cookies"; options.Authority = Configuration.GetValue\<string\>("Authority"); options.RequireHttpsMetadata = false; options.ClientId = "TPay"; options.SaveTokens = true; });** }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseDeveloperExceptionPage();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
**app.UseAuthentication();** }
    }

I even tryed to map principal HttpContexts User:

HttpContext.User = principal;

But it’s pointless, After SignInAsync   HttpContext.User IsAuthenticated is false, and after mapping principal to user, it is lost in the end. I mean when the page is called there is no data in HttpContext.

Will be great if anyone could help me and in case of questions, feel free to ask.

Hi @giorgiis,

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.

As you’ll notice, your Topic is now in the Project Development Help board. No action is needed on your part; you can continue the conversation as normal here.

Let me know if you have any other questions or if I can help with anything else.

Cheers!