Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:41:26+00:00 2026-06-06T03:41:26+00:00

Using Basic Authentication, and the asp.net Web-Api, where JSON Get/Post’s to my API, I

  • 0

Using Basic Authentication, and the asp.net Web-Api, where JSON Get/Post’s to my API, I need to check that the username/password exists in the membership table. Which the code below does:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace MvcApplication4.Filter
{
    public class BasicAuthenticationAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
            }
            else
            {
                string authToken = actionContext.Request.Headers.Authorization.Parameter;
                string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
                string username = decodedToken.Substring(0, decodedToken.IndexOf(":"));
                string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);

                if (Membership.Provider.ValidateUser(username, password))
                {
                    // User exists in the membership table
                }
                else
                {
                    // User doesn't exist - so return Unathorized
                    actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
                }
            }
        }
    }
}

I need to make reference to the username in my other controllers, to make sure the username also exists in a lookup table, which says which cars the user is allowed to query. I have a table in my database, and an associated class:

public class ApiMembers
    {
        public int id { get; set; }
        public string UserName { get; set; }
        public int car_id { get; set; }
    }


 [BasicAuthentication]
        public IEnumerable<Cars> GetCars(long id)
        {
            var auth = dba.ApiMembers.Select(a => a.car_id == id && a.UserName=***AuthorisedUserName***).FirstOrDefault();
            if (auth == null || !auth.Any())
                 {
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized));
                 }
            else
            {
                // User exists in table, so give them info on car
            }

My question is, without having to read the HTTP headers again, how do I get the UserName into the AuthorisedUserName part? Is there a way of “logging in” the user in the BasicAuthentication filter, or can you use session variables, like in webforms? Or is there a better way of knowing who has been authenticated in other controllers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:41:27+00:00Added an answer on June 6, 2026 at 3:41 am

    You could set the current principal:

    if (Membership.Provider.ValidateUser(username, password))
    {
        // User exists in the membership table
        var identity = new GenericIdentity(username);
        Thread.CurrentPrincipal = new GenericPrincipal(identity, null);
    }
    

    Now inside your ApiController you could use the User property to access the currently connected username (remark this property was added in ASP.NET MVC 4 RC – in previous versions you could use the Request.GetPrincipal extension method).

    [BasicAuthentication]
    public HttpResponseMessage Get()
    {
        string username = User.Identity.Name;
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the ASP.NET Web-Api, I have the following POST setup in my controller. When
I am using the built-in forms authentication that comes with asp.net mvc. I added
Is it possible to provide Basic authentication to clients using ASP.Net to authenticate the
I am implementing a basic authentication WCF service in ASP.NET using Visual Studio 2010.
I'm trying to use the new WCF Web API Preview 6 with Basic Authentication.
I am refactoring a working ASP.NET Web Application to expose Web Services interface using
I am using VSTS 2008 + ASP.Net + Forms Authentication + .Net 3.5 +
I am calling Assembla REST Api with basic authentication using the following code. I
I'm trying to use FireWatir (1.6.5) to access a site using Basic Authentication and
I'm trying to do Digest mostly (or Basic) Authentication using RestTemplate and httpclient (4.x)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.