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. read multiple user query - web api authentication

read multiple user query - web api authentication

Scheduled Pinned Locked Moved ASP.NET
databasesecurityhelpjson
1 Posts 1 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, I am writing to seek help, as I am getting 401 authorized error (api/values), from using the following code:

    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);
    
        }
    

    I have constructed two queries which output two different user groups, which can be seen from the user class code below:

        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 appropriate Trial role
    
    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