I work with webapi and I added a test project to a webapi project. I managed my webapi with a login bearer token and every time I call an endpoint from swagger I get the username to compare then if it matches the information inside the token.
In my test project to get that user's token I go through the Login endpoint, but when I call it I get this error:
System.NullReferenceException: 'Reference to an object not set on an object instance.'
Here is the login test and the controller login:
Test login:
[TestMethod]
public void Test_Login_ReturnToken()
{
var controller = new Controllers.AccountController
{
Request = new HttpRequestMessage(),
Configuration = new HttpConfiguration()
};
var response = controller.Login("abcde");
Assert.IsNotNull(response);
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
}
Controller Login:
[HttpPost, Route("Login")]
[AllowAnonymous]
public HttpResponseMessage Login(string nomeUtente)
{
GetLoginResponseModel responseLogin = new GetLoginResponseModel();
var command = Utils.GetOracleCommand();
command.CommandText = "SP_S_CODICE";
OracleDataReader reader = command.ExecuteReader();
var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, nomeUtente));
AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
var token = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
....
}
I believe it is due to the fact of not having a startup within the test or wrong project?
It gives me error on this instruction
var identity = new ClaimsIdentity (Startup.OAuthOptions.AuthenticationType); identity.AddClaim (new Claim (ClaimTypes.Name, userName));
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire