gpt4 book ai didi

http - HTTP GET错误用户无效

转载 作者:行者123 更新时间:2023-12-03 08:55:50 38 4
gpt4 key购买 nike

我正在使用ASP.NET,在其中我通过动词GET接收查询,该应用程序专用于该URL。

该代码有效,但是如果用户发送的密码使http 200无效,请回答我,并在消息的正文中显示“Fail user or pasword”。

namespace WebApp_dev.Controllers
{

public class ValuesController : ApiController
{


[HttpGet]
public string Save(string point)
{

// in point variable you will get part between /dev and /save
// for /dev/point/save?name=125.25 url it will be "point"

// get parameters that follow by ? mark in url

var queryParams = GetQueryParams(Request);
string url_completa = Request.RequestUri.AbsolutePath;

int _indexPto = url_completa.IndexOf("/dev/");
url_completa=url_completa.Remove(0, (_indexPto + 5));
// _indexPto = url_completa.IndexOf("/");
url_completa = url_completa.Remove(url_completa.IndexOf("/"), (url_completa.Length - url_completa.IndexOf("/"))); // tengo el nombre del punto en url_completa
//--------------------------------------------------
string url_query = Request.RequestUri.Query;
_indexPto = url_query.IndexOf("&u=");
url_query = url_query.Remove(0, _indexPto+1);
string[] _User_pasw = url_query.Split(char.Parse("&"));//con esto queda la cadena separada el usuario y contraseña
string _usuario, _pasword;
_usuario = _User_pasw[0].Split(char.Parse("="))[1];
_pasword = _User_pasw[1].Split(char.Parse("="))[1];
bool _usuarioValido = UsuarioValido(_usuario, _pasword);



if (_usuarioValido == false)
{
return "Fail user or pasword";
}

// loop through all of them
foreach (var pair in queryParams)
{
string paramName = pair.Key; // for /dev/point/save?name=125.25 will be "name"
string paramvalue = queryParams[pair.Key]; // for /dev/point/save?name=125.25 will be 125.25

AlmacenarValor(url_completa.ToString(), paramvalue, paramName);
}

return "OK: " + url_completa.ToString();
}

private bool UsuarioValido(string _usuario, string _pasword)
{
MonitoreoEntities _context = new MonitoreoEntities();
PuntoDeMedicion _pdm = _context.PuntoDeMedicion.FirstOrDefault(a => a.Nombre == _usuario && a.Contrasena == _pasword);
if (_pdm == null)
return false;
else
return true;
}

private Dictionary<string, string> GetQueryParams(HttpRequestMessage request)
{

return request.GetQueryNameValuePairs()
.ToDictionary(kv => kv.Key, kv => kv.Value,
StringComparer.OrdinalIgnoreCase);
}
public static void AlmacenarValor(string _dispositivo, string _valor, string _parametro)
{

MonitoreoEntities _context = new MonitoreoEntities();
PuntoDeMedicion _puntoDeMedicion = _context.PuntoDeMedicion.Include("EntradaSalida").Where(a => a.Nombre == _dispositivo).FirstOrDefault();
foreach (EntradaSalida _entradasalida in _puntoDeMedicion.EntradaSalida)
{
if (_entradasalida.Etiqueta == _parametro)
{
Registro _registro = new Registro()
{
EntradaSalidaId = _entradasalida.Id,
FechaHoraCreacion = DateTime.Now,
FechaHoraRegistro = DateTime.Now,
PuntoDeMedicionId = _puntoDeMedicion.Id,
Valor = decimal.Parse(_valor)
};
_context.Registro.Add(_registro);
_context.SaveChanges();
break;
}
}
}
}
}

对Google Chrome使用高级REST客户端:

响应用户确定(密码确定)
200 OK Show explanation Loading time: 8723
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: text/plain; charset=utf-8
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Response headers
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcUHJveWVjdG8gdGVsZW1ldHJpYVxTZXJ2ZXJcV2ViQXBwX2RldlxkZXZccHJ1ZWJhMTIzXHNhdmU=?=
X-Powered-By: ASP.NET
Date: Mon, 22 Sep 2014 22:23:01 GMT
Content-Length: 28
Raw
JSON
Response
Word wrap Copy to clipboard Save as file

" OK: list123"



响应用户密码无效
200 OK Show explanation Loading time: 1415
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: text/plain; charset=utf-8
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Response headers
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcUHJveWVjdG8gdGVsZW1ldHJpYVxTZXJ2ZXJcV2ViQXBwX2RldlxkZXZccHJ1ZWJhMTIzXHNhdmU=?=
X-Powered-By: ASP.NET
Date: Mon, 22 Sep 2014 22:30:14 GMT
Content-Length: 18
Raw
JSON
Response
Copy to clipboard Save as file

"Fail user or pasword"



它是以这种方式编程的,可以正常工作,如果用户或密码不正确并且http 200 Ok是正确的,我想更改此消息并发送HTTP 409冲突。

非常感谢您的回复

我遇到了一系列错误,它们是另一个上下文中的变量:

*

_usuarioValido
url_completa



*

我可以将其从一个上下文传递到另一个上下文。
代码仍然如下:
    public class ValuesController : ApiController
{


[HttpGet]
public string Save(string point)
{

// in point variable you will get part between /dev and /save
// for /dev/point/save?name=125.25 url it will be "point"

// get parameters that follow by ? mark in url

var queryParams = GetQueryParams(Request);
string url_completa = Request.RequestUri.AbsolutePath;
// Busco el nombre del punto de medicion-----------
int _indexPto = url_completa.IndexOf("/dev/");
url_completa=url_completa.Remove(0, (_indexPto + 5));
// _indexPto = url_completa.IndexOf("/");
url_completa = url_completa.Remove(url_completa.IndexOf("/"), (url_completa.Length - url_completa.IndexOf("/"))); // tengo el nombre del punto en url_completa
//--------------------------------------------------
string url_query = Request.RequestUri.Query;
_indexPto = url_query.IndexOf("&u=");
url_query = url_query.Remove(0, _indexPto+1);
string[] _User_pasw = url_query.Split(char.Parse("&"));//con esto queda la cadena separada el usuario y contraseña
string _usuario, _pasword;
_usuario = _User_pasw[0].Split(char.Parse("="))[1];
_pasword = _User_pasw[1].Split(char.Parse("="))[1];
bool _usuarioValido = UsuarioValido(_usuario, _pasword);



if (_usuarioValido == false)
{
return "Usuario invalido";
}

// loop through all of them
foreach (var pair in queryParams)
{
string paramName = pair.Key; // for /dev/point/save?name=125.25 will be "name"
string paramvalue = queryParams[pair.Key]; // for /dev/point/save?name=125.25 will be 125.25

AlmacenarValor(url_completa.ToString(), paramvalue, paramName);
}

return "SUCCESS OK de: " + url_completa.ToString()+"\n";// mando el ok, tiene que llevar el fin de linea.
}

[HttpGet]
[ResponseType(typeof(String))]
public HttpResponseMessage Save(HttpRequestMessage request, string point)
{

if (_usuarioValido == false)
{
return request.CreateResponse(HttpStatusCode.Conflict, "Fail user or password");
}

return request.CreateResponse(HttpStatusCode.OK, url_completa.ToString());
}

最佳答案

让我们尝试一下:

[HttpGet]
[ResponseType(typeof(String))]
public HttpResponseMessage Save(HttpRequestMessage request, string point)
{
.......
if (_usuarioValido == false)
{
return request.CreateResponse(HttpStatusCode.Conflict, "Fail user or password");
}

.......
return request.CreateResponse(HttpStatusCode.OK, url_completa.ToString());
}

我没有测试上面的代码,但是大概有这个概念。

希望能帮助到你。

谢谢!

关于http - HTTP GET错误用户无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25984242/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com