1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to retrieve windows user while sending angular request to .Net Core 8?

Discussão em 'Angular' iniciado por user6009865, Outubro 17, 2024 às 18:43.

  1. user6009865

    user6009865 Guest

    I've been struggling too much on this, I have an Angular application with .Net Core 8 and all I want is to get the windows user from a client request because I need to log who makes certain action.

    I have tried too many things and none of them work, I always get isAuthenticated = false, here is my code, can somebody help me?

    Program.cs

    var builder = WebApplication.CreateBuilder(args);

    builder.Services.AddControllers();
    builder.Services.AddHttpContextAccessor();
    ...
    app.UseAuthentication();
    app.UseAuthorization();


    launchingSettings.json

    {
    "$schema": "http://json.schemastore.org/launchsettings.json",
    "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
    "applicationUrl": "http://localhost:2390",
    "sslPort": 0
    }
    },
    "profiles": {
    "http": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": true,
    "applicationUrl": "http://localhost:5015",
    "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
    }
    },
    "IIS Express": {
    "commandName": "IISExpress",
    "launchBrowser": true,
    "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
    }
    }
    }
    }


    proxy.config.js

    const { env } = require('process');

    const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
    env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:5015';

    const PROXY_CONFIG = [
    {
    context: [
    "/api/MyApi",
    ],
    target,
    secure: false,
    onProxyRes: proxyRes => {
    const key = "www-authenticate";
    proxyRes.headers[key] = proxyRes.headers[key] &&
    proxyRes.headers[key].split(",");
    }
    }
    ]

    module.exports = PROXY_CONFIG;



    MyApiController.cs

    public MyApiController(IHttpContextAccessor httpContextAccessor)
    {
    this.httpContextAccessor = httpContextAccessor;
    }

    [HttpGet]
    public List<string> GetMyApi()
    {
    //Here I need to get the windows user and I always get isAuthenticated = false
    //...
    }


    I'm also passing withCredentials = true when calling the api

    this.http.get<string>[]>('/api/MyApi', { withCredentials: true })

    This is the info when calling the api, I'm not sure if the authentication is in fact being passed, one thing I noticed is that server says: "Kestrel" and I read that there is no windows authentication using that, is that true? Would that be the issue? I also tried using IIS Express but then I get 500 error.

    [​IMG]

    Let me know if you need more info, I would also appreciate if you can point me to the right direction, I'm new in .Net Core by the way.

    Continue reading...

Compartilhe esta Página