Search code examples
postmanwindows-authenticationrestsharpntlm-authentication

Windows Authentication for Postman Restsharp


Currently,

I using the Postman Restsharp to retrieve the JWT token in iphone Safari mobile.

May I ask about how to add the Windows authentication?

I had try the method as below, but I can't retrieve the response data.

But its work at Company Laptop.

var client = new RestClient("https://mvponduty.com/XChange_App_Api/api/auth/authenticate");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.UseDefaultCredentials = true;
request.AddHeader("Content-Type", " application/json; charset=utf-8");
request.AddHeader("Access-Control-Allow-Origin", " *");
request.AddHeader("Cache-Control", " no-cache");
request.AddHeader("Pragma", " no-cache");
request.AddHeader("Cookie", ".AspNetCore.Session=CfDJ8AoxL%2BrAgZtDtnzh7ZplJfx4moiRGYT4rFtA6zp%2B1d8CAQ4D%2FZary6IvN%2Ftjm7wl2Ke992wbhnP7cmv3q7ba0%2BQoZKHuT%2B2FyJzIAjaJFH2GX0ZFiz1nHdSLyGhPMrG4KCNXZ94u79I7DHKjVAxcREfISUxxrFnxib0Aw1Q4yNQw");
request.AddParameter(" application/json; charset=utf-8", "",  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Solution

  • I use another method to generate JWT Token.

      string key = "iNivDmHLpUA223sqsfhqGbMRdRj1PVkH";
    
                // Create Security key  using private key above:
                // not that latest version of JWT using Microsoft namespace instead of System
                var securityKey = new Microsoft
                   .IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
    
                // Also note that securityKey length should be >256b
                // so you have to make sure that your private key has a proper length
                //
                var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
                                  (securityKey, SecurityAlgorithms.HmacSha256Signature);
    
                //  Finally create a Token
                var header = new JwtHeader(credentials);
    
                //Some PayLoad that contain information about the  customer
                var payload = new JwtPayload
               {
                    { "sub", "hello "},
                    { "scope", "http://dummy.com/"},
    };
    
         
                var secToken = new JwtSecurityToken(header, payload);
                var handler = new JwtSecurityTokenHandler();
    
                // Token to String so you can use it in your client
                var tokenString = handler.WriteToken(secToken);
    
              
    
                Console.WriteLine(tokenString);