Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am trying to integrate user class method into basic auth class and I am currently experiencing --'bool' does not contain a definition for 'username' and no extension method 'username' accepting a first argument of type 'bool' could be found (are you missing) -- error.

IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);

basic auth class:
C#
public BasicAuthHandler(iUser repository)
        {
            this.repository = repository;
        }

        [Inject]
        iUser repository { get; set; }


        protected override Task<HttpResponseMessage> 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")),
                };
            }

        
            var user = repository.full(credentials[0], credentials[1]);
            if (user == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("access denied")),
                };

            }

            
            //var identity = CreateIdentity(user);
            IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;


user class
C#
public bool full(string username, string password)
{

    var query = from s in db.Subscriptions
                join u in db.UserDetails on s.sUID equals u.uID
                where s.sPriceABS_ExpiryDate >= DateTime.Now &&
                s.sPID.Value == 163 &&
                s.All.Value == true &&
                u.uUsername == username &&
                u.uPassword == password
                select u; //

    // "execute" the query
    return query.FirstOrDefault() != null;

}


Is this correct approach. Please help. Many thanks.
Posted
Comments
Sanket Saxena 13-Mar-14 13:16pm    
Which line you are getting ERROR?
miss786 13-Mar-14 13:17pm    
IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);

The 'user.username' parameter is throwing the issue.

Thank you for reply.

In the code below, the call to repository.full returns a boolean not a user:
var user = repository.full(credentials[0], credentials[1]);

Later you are using your user var like it is a user object and attempting to reference the username property thus your error:
IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);

Should it be repository.username?
 
Share this answer
 
Comments
miss786 13-Mar-14 13:43pm    
Hi, thank you for your response. I have tried your suggestion before, the 'repository.username' gives me error -- does not contain definition for username' -- error. Any help is most welcomed.
Gopherdude 13-Mar-14 14:40pm    
You can use credentials[0] since that is what you are passing to the repository.full() method as the username.
Gopherdude 13-Mar-14 14:45pm    
Also note, that since repository.full() returns a boolean, your "if(user == null)..." block will never execute.
First please check What is your method returning, i mean query variable? is it true or false seems like not?:

C#
public bool full(string username, string password)
        {
            var query = from s in db.Subscriptions
                        join u in db.UserDetails on s.sUID equals u.uID
                        where s.sPriceABS_ExpiryDate >= DateTime.Now &&
                        s.sPID.Value == 163 &&
                        s.All.Value == true &&
                        u.uUsername == username &&
                        u.uPassword == password
                        select u; //

            // "execute" the query
            return query.FirstOrDefault() != null; 
 
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900