简体   繁体   中英

C# ASP.NET Core 2.1 Linux NTLM Apache 2.2 - Get username?

We are new to .NET Core. We have a ASP.NET Core 2.1 application which is hosted by our Systems Administration Team on Linux Server using Apache 2.2 Proxy. The users will launch our application URL in browser and Apache proxy will receive the request and forward it to our application on Linux Server.

Everything works fine and we are able to do continous development and deployments on our own. Now we started to secure our application and our firm decided to add Windows Authentication to our application. After Systems Administration team did their work we started to get below key values in Request Header.

We are trying to write C# code in our ASP.NET Core 2.1 application to get username. We feel that value of Key: Authorization which is encrypted will have username in it. We tried so many ways to decrypt it but cannot decrypt it.

Can someone please help and let us know how to get username in this case.

Please see below code which I am trying.

if (!Request.Headers.ContainsKey("Authorization"))
{
    string cookieValue = Request.Headers["Authorization"];
    cookieValue= cookieValue.Substring("NTLM ".Length).Trim();
    UTF8Encoding specialUtf8Encoding = new UTF8Encoding(false, true);

    // below code do not work. How to get plain text ?
    //byte[] protectedBytes = Base64UrlTextEncoder.Decode(cookieValue);
    //string plainText = System.Text.ASCIIEncoding.ASCII.GetString(protectedBytes);
} 

Below is what we see in Request Header key and values

Key: Cache-Control, Value=max-age=0
Key: Connection, Value=Keep-Alive
Key: Accept, Value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng
Key: Accept-Encoding, Value=gzip, deflate, br
Key: Accept-Language, Value=en-US,en;q=0.9
Key: Authorization, Value=NTLM TlRMTVNTUAADAAAAGAAYAIAAAABUAVQBmAAAAAwADABYAAAACAAIAGQAAAAUABQAbAAAAAAAAADsAQAABYKIogoA7kIAAAAPNSOWmAbXlPi5fhYGSO54RVAATQBBAF8ATgBCAHAAYwBhAG8AQQBOAFAAWABEAFcAVAAxADYANgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAhmacrZwRfdlIkhEBBfdWjAQEAAAAAAACNce03PTXWAcU/7pwxmsPkAAAAAAIADABQAE0AQQBfAE4AQgABABAAQQBOAFQAVgBQAFcAUwAxAAQAJgBhAG0AZgAuAHAAYQBjAGkAZgBpAGMAbABpAGYAZQAuAG4A
Key: Cookie, Value=.AspNetCore.Antiforgery.Xf_oDoHBPRA=CfDJ8LQZvjci-adCv0t9XQ2PRfiQ6oFCKJDXb8Xe8d7Gd6wOtJc97d7fVTEUt8xrxjk9XYfqmyeGyO7iLAbWLKRTGPUVo9v2_zoRnCqVSrADnZPhBToSzxuoLf9u2QNcFTvkbYEOaNvphVotB4saPlb_osw
Key: Host, Value=dev.myweb.net:4443
Key: User-Agent, Value=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
Key: Upgrade-Insecure-Requests, Value=1
Key: Sec-Fetch-Site, Value=none
Key: Sec-Fetch-Mode, Value=navigate
Key: Sec-Fetch-User, Value=?1
Key: Sec-Fetch-Dest, Value=document
Key: site, Value=dev.myweb.net
Key: port, Value=443
Key: X-Forwarded-For, Value=11.123.13.456
Key: X-Forwarded-Host, Value=dev.myweb.net:4443
Key: X-Forwarded-Server, Value=dev.myweb.net

I am just a developer but not system administrator. After doing alot of reading, reseach, I am able to solve the issue. First I narrowed it down to Aapche server version and what ever I tried was solution to Apache 2.4 but our's is Apache 2.2 .

In Apache 2.2 server we gave below configuration.

   <LocationMatch ^/mylocation>
     AuthName "NTLM Authentication"
     NTLMAuth on
     NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
     NTLMBasicAuthoritative on
     NTLMBasicRealm xxx_yy
     AuthType NTLM
     require valid-user
     RewriteCond %{LA-U:REMOTE_USER} (.+)
     RewriteRule . - [E=RU:%1]
     RequestHeader set X-Remote-User %{RU}e

   </LocationMatch>

In our C# ASP.NET Core 2.1 application we get below in HTTP Request Header for both http and https calls.

    Key: Cache-Control, Value=max-age=0
    Key: Connection, Value=Keep-Alive
    Key: Accept, Value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng
    Key: Accept-Encoding, Value=gzip, deflate, br
    Key: Accept-Language, Value=en-US,en;q=0.9
    Key: Authorization, Value=NTLM TlRMTVNTUAADAAAAGAAYAIAAAABUAVQBmAAAAAwADABYAAAACAAIAGQAAAAUABQAbAAAAAAAAADsAQAABYKIogoA7kIAAAAPNSOWmAbXlPi5fhYGSO54RVAATQBBAF8ATgBCAHAAYwBhAG8AQQBOAFAAWABEAFcAVAAxADYANgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAhmacrZwRfdlIkhEBBfdWjAQEAAAAAAACNce03PTXWAcU/7pwxmsPkAAAAAAIADABQAE0AQQBfAE4AQgABABAAQQBOAFQAVgBQAFcAUwAxAAQAJgBhAG0AZgAuAHAAYQBjAGkAZgBpAGMAbABpAGYAZQAuAG4A
    Key: Cookie, Value=.AspNetCore.Antiforgery.Xf_oDoHBPRA=CfDJ8LQZvjci-adCv0t9XQ2PRfiQ6oFCKJDXb8Xe8d7Gd6wOtJc97d7fVTEUt8xrxjk9XYfqmyeGyO7iLAbWLKRTGPUVo9v2_zoRnCqVSrADnZPhBToSzxuoLf9u2QNcFTvkbYEOaNvphVotB4saPlb_osw
    Key: Host, Value=dev.myweb.net:4443
    Key: User-Agent, Value=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
    Key: Upgrade-Insecure-Requests, Value=1
    Key: Sec-Fetch-Site, Value=none
    Key: Sec-Fetch-Mode, Value=navigate
    Key: Sec-Fetch-User, Value=?1
    Key: Sec-Fetch-Dest, Value=document
    Key: site, Value=dev.myweb.net
    Key: port, Value=443
    Key: X-Forwarded-For, Value=11.123.13.456
    Key: X-Forwarded-Host, Value=dev.myweb.net:4443
    Key: X-Forwarded-Server, Value=dev.myweb.net
    Key: X-Remote-User, Value=xxx_yy\abcdefg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM