Я новичок в API и очень смущен, я ввел базовую аутентификацию, она аутентифицирует пользователя, и я получаю 200 статус, но тогда почему он не достигает моего контроллера?
мой веб-APIconfig:
config.Filters.Add(new BasicAuthenticationAttribute());
// config.Filters.Add(new AuthorizeAttribute());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
мой контроллер:
[BasicAuthentication]
public HttpResponseMessage Get(string id, string dttimeFrom, string dttimeTo)
{
var _context = new TenMinutesContext();
var rslt = (from m in _context.users
// where m.m_turbine_id == id
select new User
{
m_user_name = m.m_user_name
// m.m_voltage_u12=
}).Take(10).ToList();
return Request.CreateResponse(HttpStatusCode.OK,
rslt);
в почтальоне я положил http://localhost:51154/api/values?id=192.168.39.82&dttimeFrom=05102019&dttimeTo=10102019
и я получаю аутентификацию пользователя, но почему яне могу увидеть результат контроллера?
мой класс BasicAuthentication:
public class BasicAuthenticationAttribute: AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
var authHeader = actionContext.Request.Headers.Authorization;
if (authHeader != null)
{
var authenticationToken = actionContext.Request.Headers.Authorization.Parameter;
var decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
var usernamePasswordArray = decodedAuthenticationToken.Split(':');
var userName = usernamePasswordArray[0];
var password = usernamePasswordArray[1];
// Replace this with your own system of security / means of validating credentials
var isValid = userName == "rene" && password == "2019";
if (isValid)
{
var principal = new GenericPrincipal(new GenericIdentity(userName), null);
Thread.CurrentPrincipal = principal;
actionContext.Response =
actionContext.Request.CreateResponse(HttpStatusCode.OK,
"User " + userName + " successfully authenticated");
base.OnAuthorization(actionContext);
}
else
{
HandleUnathorized(actionContext);
}
}
}
private static void HandleUnathorized(HttpActionContext actionContext)
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
actionContext.Response.Headers.Add("WWW-Authenticate", "Basic Scheme='Data' location = 'http://localhost:");
}
}
}