Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. web api query - 401 error

web api query - 401 error

Scheduled Pinned Locked Moved ASP.NET
databasesecurityjsonhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    Dear all, When I call 'api/values', it only allows me login as 'full' user details, but if a login as 'trial' user login details, it throws a 401 error. I would like to ask, if I am missing something in the queries in the user class below or the basicAuthn class:

    protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
    AuthenticationHeaderValue authValue = request.Headers.Authorization;

            if (authValue == null || authValue.Scheme != BasicAuthResponseHeaderValue)
            {
                return Unauthorized(request);
            }
    
            string\[\] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authValue.Parameter)).Split(new\[\] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials\[0\]) || string.IsNullOrEmpty(credentials\[1\]))
            {
                //return Unauthorized(request);
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("access denied")),
                };
            }
    
            ClaimRole user = repository.trial(credentials\[0\], credentials\[1\]);
    		ClaimRole user2 = repository.unlim(credentials\[0\], credentials\[1\]);
    
                if (user == null || user2  == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("access denied")),
                };
    
            }
            else
            {
    
               IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.Username, BasicAuthResponseHeaderValue), new string\[\] { user.role });
                Thread.CurrentPrincipal = principal;
                HttpContext.Current.User = principal;
            }
    
            return base.SendAsync(request, cancellationToken);
    
        }
    

    User Class

    public ClaimRole trial(string username, string password)
    {
    var query = (from s in db.subs
    join u in db.user on s.sUID equals u.uID
    where s.sExpiryDate >= DateTime.Now &&
    u.uUsername == username &&
    u.uPassword == password
    select u).FirstOrDefault();

            if (query != null)
            {
                // Build a user and add the ap
    
    B 1 Reply Last reply
    0
    • M miss786

      Dear all, When I call 'api/values', it only allows me login as 'full' user details, but if a login as 'trial' user login details, it throws a 401 error. I would like to ask, if I am missing something in the queries in the user class below or the basicAuthn class:

      protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
      AuthenticationHeaderValue authValue = request.Headers.Authorization;

              if (authValue == null || authValue.Scheme != BasicAuthResponseHeaderValue)
              {
                  return Unauthorized(request);
              }
      
              string\[\] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authValue.Parameter)).Split(new\[\] { ':' });
              if (credentials.Length != 2 || string.IsNullOrEmpty(credentials\[0\]) || string.IsNullOrEmpty(credentials\[1\]))
              {
                  //return Unauthorized(request);
                  var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                  {
                      Content = new StringContent(string.Format("access denied")),
                  };
              }
      
              ClaimRole user = repository.trial(credentials\[0\], credentials\[1\]);
      		ClaimRole user2 = repository.unlim(credentials\[0\], credentials\[1\]);
      
                  if (user == null || user2  == null)
              {
                  var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                  {
                      Content = new StringContent(string.Format("access denied")),
                  };
      
              }
              else
              {
      
                 IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.Username, BasicAuthResponseHeaderValue), new string\[\] { user.role });
                  Thread.CurrentPrincipal = principal;
                  HttpContext.Current.User = principal;
              }
      
              return base.SendAsync(request, cancellationToken);
      
          }
      

      User Class

      public ClaimRole trial(string username, string password)
      {
      var query = (from s in db.subs
      join u in db.user on s.sUID equals u.uID
      where s.sExpiryDate >= DateTime.Now &&
      u.uUsername == username &&
      u.uPassword == password
      select u).FirstOrDefault();

              if (query != null)
              {
                  // Build a user and add the ap
      
      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      I guess the problem is here:

      ClaimRole user = repository.trial(credentials[0], credentials[1]);
      ClaimRole user2 = repository.unlim(credentials[0], credentials[1]);
      if (user == null || user2 == null)

      If the user does not have BOTH the trial and unlim priviledges, you send the Access Denied message. Use && isntead of ||.

      M 1 Reply Last reply
      0
      • B Bernhard Hiller

        I guess the problem is here:

        ClaimRole user = repository.trial(credentials[0], credentials[1]);
        ClaimRole user2 = repository.unlim(credentials[0], credentials[1]);
        if (user == null || user2 == null)

        If the user does not have BOTH the trial and unlim priviledges, you send the Access Denied message. Use && isntead of ||.

        M Offline
        M Offline
        miss786
        wrote on last edited by
        #3

        Thank you so much for your response and help. I am sorry to inform, that i have tried using the '&&' operator but I am still getting the same 401 error, when logging in using full query user details. i would like to ask, if their is certain parameter and method, i should be looking into while debugging the code. I really appreciated your help. please advise. Many thanks.

        B 1 Reply Last reply
        0
        • M miss786

          Thank you so much for your response and help. I am sorry to inform, that i have tried using the '&&' operator but I am still getting the same 401 error, when logging in using full query user details. i would like to ask, if their is certain parameter and method, i should be looking into while debugging the code. I really appreciated your help. please advise. Many thanks.

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          then test those two repository.trail / repository.unlim functions. At least one of them has a bug.

          M 1 Reply Last reply
          0
          • B Bernhard Hiller

            then test those two repository.trail / repository.unlim functions. At least one of them has a bug.

            M Offline
            M Offline
            miss786
            wrote on last edited by
            #5

            Thank you for your feedback. i am stuck on little status code issue, regarding this long problem. When I debug the full and trial repository, while login as 'trail' user, 'resp' variable below is highlighted as red with a 404 status code, but on the fiddler testing environment, it displays the url as 401 unauthorized issue.

                    ClaimRole user = repository.trial(credentials\[0\], credentials\[1\]);
                   ClaimRole user2 = repository.full(credentials\[0\], credentials\[1\]);
            
                        if (user == null || user2  == null)
                    {
                        var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                        {
                            Content = new StringContent(string.Format("access denied")),
                        };
            
                    **}**// this bracket is highlighted while debugging through which the local window shows 404 status code
                    else
                    {
            

            resp -- {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StringContent, Headers: Hence I am little unclear, which error to look into. if you get time, could you please advice on any material i should look into or concentrate on. Many thanks for your time and help.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups