Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have two queries in my repository class and each of them call different group of users. I would like to call the two queries in my authentication method below, but I am little unsure, how would I go about doing this so? I currently have one query calling authentication method below but I would like to know, how can I call both of the queries. Would I need to group my queries together, if so, how?

Repository class:
C#
public Cust trial_test(string username, string password)
      {
          var query = from s in db.Subscriptions
                      join u in db.UserDetails on s.sUID equals u.uID
                      where s.ExpiryDate >= DateTime.Now &&
                      s.sPID.Value == 163 &&
                      s.All.Value != true &&
                      u.uUsername == username &&
                      u.uPassword == password
                      select u;
          return query.FirstOrDefault();
      }

      public Cust full_test(string username, string password)
      {
          var query = from s in db.Subscriptions
                      join u in db.UserDetails on s.sUID equals u.uID
                      where s.ExpiryDate >= DateTime.Now &&
                      s.sPID.Value == 163 &&
                      s.All.Value == true &&
                      u.uUsername == username &&
                      u.uPassword == password
                      select u;
          return query.FirstOrDefault();
      }


Authentication method:
C#
Cust user = repository.trial_test(credentials[0], credentials[1]);

                if (user == null)
                {
                var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent(string.Format("access denied")),
                };
                return Unauthorized(request);
            }
            else
            {
                IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.Username, BasicAuthResponseHeaderValue), null);
                Thread.CurrentPrincipal = principal;
                HttpContext.Current.User = principal;
            }
            return base.SendAsync(request, cancellationToken);


Please help. Any advice, would be very much appreciated. Many thanks.
Posted

try to make use of optional parameter in your function like this

public Cust trial_test(string username, string password, int group = 0)
       {
           if (group == 0)
           {

               var query = from s in db.Subscriptions
                           join u in db.UserDetails on s.sUID equals u.uID
                           where s.ExpiryDate >= DateTime.Now &&
                           s.sPID.Value == 163 &&
                           s.All.Value != true &&
                           u.uUsername == username &&
                           u.uPassword == password
                           select u;
               return query.FirstOrDefault();
           }
           else
           {
               var query = from s in db.Subscriptions
                           join u in db.UserDetails on s.sUID equals u.uID
                           where s.ExpiryDate >= DateTime.Now &&
                           s.sPID.Value == 163 &&
                           s.All.Value == true &&
                           u.uUsername == username &&
                           u.uPassword == password
                           select u;
               return query.FirstOrDefault();
           }
       }



       Cust user = repository.trial_test(credentials[0], credentials[1],group:1);

//pass the value 1 to the group to execute the second query else it will call the first one.
 
Share this answer
 
Comments
miss786 23-Apr-14 6:55am    
Many thanks for your solution. Is their a way to manipulate the group parameter to be able to login using both trial and full user credentials. I am assuming it would require an 'or' operator but i am little unsure where would add it. please advice. Many thanks in advance.
Er Daljeet Singh 23-Apr-14 7:02am    
Its not clear to us tell us what u exactly want to do..
in both the queries only difference is the
s.All.Value != true
clause. If this is the case then one linq like below will work, considering s.All.Value is either true or false
public Cust trial_test(string username, string password, bool spidFlag)
      {


              var query = from s in db.Subscriptions
                          join u in db.UserDetails on s.sUID equals u.uID
                          where s.ExpiryDate >= DateTime.Now &&
                          s.sPID.Value == 163 &&
                          s.All.Value == spidFlag &&
                          u.uUsername == username &&
                          u.uPassword == password
                          select u;
              return query.FirstOrDefault();

      }
 
Share this answer
 

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