read multiple user query - web api authentication
ASP.NET
1
Posts
1
Posters
0
Views
1
Watching
-
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