How to set header using IHttpActionResult - asp.net-mvc

i want to add token to header for webapi request.
how can i do this ?
this is my sample code
public IHttpActionResult Authenticate(Login data)
{
var Token = "fdsf123546fskjhf-gsuyuhsh";
//here add these token to header
return ?
}
thank you.

You could use ResponseMessageResult class for that:
public IHttpActionResult Authenticate(Login data)
{
var Token = "fdsf123546fskjhf-gsuyuhsh";
var response = new HttpResponseMessage();
response.Headers.Add("Authentication", Token);
return new ResponseMessageResult(response);
}
Or create your own result class:
public class HeaderActionResult : IHttpActionResult
{
private Tuple<string, string> header;
public HeaderActionResult(Tuple<string, string> header)
{
this.header = header;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage();
response.Headers.Add(header.Item1, header.Item2);
return Task.FromResult<HttpResponseMessage>(response);
}
}
public IHttpActionResult Authenticate(Login data)
{
var Token = "fdsf123546fskjhf-gsuyuhsh";
return new HeaderActionResult(Tuple.Create("Authentication", Token));
}
Or add a header to any existing IHttpActionResult object using an extension method:
public static class HttpActionResultExtensions
{
public static IHttpActionResult AddHeader(this IHttpActionResult actionResult, string name, string value)
{
return new HeaderActionResult(actionResult, name, value);
}
private class HeaderActionResult : IHttpActionResult
{
private readonly IHttpActionResult actionResult;
private string name;
private string value;
public HeaderActionResult(IHttpActionResult actionResult, string name, string value)
{
this.actionResult = actionResult;
this.name = name;
this.value = value;
}
public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage httpResponseMessage = await this.actionResult.ExecuteAsync(cancellationToken);
httpResponseMessage.Headers.Add(this.name, this.value);
return httpResponseMessage;
}
}
}

This code working me
public IHttpActionResult Authenticate(Login data)
{
Status = "success";
Message = "You have been Authenticated successfully";
var Token = "fdsf123546fskjhf-gsuyuhsh";
var Result = new { Status = Status, Message = Message };
var Response = Request.CreateResponse(HttpStatusCode.OK, Result);
Response.Headers.Add("AccessToken", Token.AccessToken);
return ResponseMessage(Response);
}

Related

Some services are not able to be constructed Error while validating the service descriptor Lifetime: Scoped Unable to resolve service for type

my usermanager services is :
public class UserManagerService : IUserManagerService
{
private readonly UserManager<UserModel> userManager;
private readonly UserServiceHelper userServiceHelper;
public UserManagerService(UserManager<UserModel> _userManager)
{
this.userManager = _userManager;
userServiceHelper = new UserServiceHelper();
}
public async Task<bool> CreateUser(UserViewModel user)
{
try
{
var new_user = userServiceHelper.GetNewItems(user);
await userManager.CreateAsync(new_user);
return true;
}
catch(Exception e)
{
throw;
}
}
IUsermanagerServices interface is :
public interface IUsermanagerServices
{
Task<bool> CreateUser(UserViewModel user);
Task<bool> CreateUserBatch(List<UserViewModel> users);
Task<bool> DeleteUser(UserViewModel user);
}
And userapicontroller is :
[Route("api/[controller]")]
[ApiController]
public class UserApiController : ControllerBase
{
private readonly IUserManagerService userManager;
public UserApiController(IUserManagerService _userManager)
{
userManager = _userManager;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] List<UserViewModel> users)
{
try
{
var _result = await userManager.CreateUserBatch(users);
var _response = new ResponseModel<bool>()
{
Content = _result,
Message = "np"
};
return Ok(_response);
}
catch(Exception e)
{
var _response = new ResponseModel<bool>()
{
Content = false,
ExceptionMessage = e.Message,
HasError = true
}.ToString();
return Problem(_response);
}
}
service extension is :
public static void AddUserManagerServices(this IServiceCollection services)
{
services.AddScoped<IUserManagerService, UserManagerService>();
}
and program :
#region
builder.Services.AddUserManagerServices();
#endregion
var app = builder.Build();
in this last line i have exception
Some services are not able to be constructed (Error while validating the service
descriptor 'ServiceType: Lifetime: Scoped ImplementationType:
Unable to resolve service for type while attempting to activate)
how can i fix it ?

Webapi Response 500 Internal Server Error

I made an asp.net core web api and implemented some logic to register user.
But the issue is that whenever I call the controller from postman, it gives me 500 Internal Server Error.I also implemented an simple IActionResult to return welcome message, that also don't work. Here is my code.
[ApiController]
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthRepository _repo;
public AuthController(IAuthRepository repo)
{
_repo = repo;
}
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody]CreateUserDTO createUserDto)
{
createUserDto.Username=createUserDto.Username.ToLower();
if(await _repo.UserExists(createUserDto.Username))
return BadRequest("User With This User Name Already Exists");
var userToCreate=new User
{
UserName=createUserDto.Username
};
var createdUser=await _repo.Register(userToCreate,createUserDto.Password);
return Ok("user registered");
}
[HttpGet]
public IActionResult Get()
{
var message="welcome message";
return Ok(message);
}
}
Looking to hear from you soon.
Thanks
I figured out the problem.
The problem was with my AuthRepository constructor where I wasn't specifying the public modifier.
Here is my AuthRepository code (working)
public class AuthRepository : IAuthRepository
{
private readonly ApplicationDatabase _db;
public AuthRepository(ApplicationDatabase db)
{
_db = db;
}
public async Task<User> Login(string username, string password)
{
var user=await _db.Users.Where(x=>x.UserName==username).FirstOrDefaultAsync();
if (user==null)
return null;
if(!verifyPasswordHash(password,user.PasswordHash,user.PasswordSalt))
return null;
return user;
}
private bool verifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt)
{
using(var hmac=new System.Security.Cryptography.HMACSHA512(passwordSalt))
{
var computedPasswordHash=hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
for (int i = 0; i < computedPasswordHash.Length; i++)
{
if (computedPasswordHash[i]!=passwordHash[i])
{
return false;
}
}
}
return true;
}
public async Task<User> Register(User user, string password)
{
byte[] passwordHash,passwordSalt;
CreatePasswordHashSalt(password,out passwordHash,out passwordSalt);
user.PasswordHash=passwordHash;
user.PasswordSalt=passwordSalt;
await _db.Users.AddAsync(user);
_db.SaveChangesAsync();
return user;
}
private void CreatePasswordHashSalt(string password, out byte[] passwordHash, out byte[] passwordSalt)
{
using(var hmac=new System.Security.Cryptography.HMACSHA512())
{
passwordSalt=hmac.Key;
passwordHash=hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
}
}
public async Task<bool> UserExists(string username)
{
if(await _db.Users.AnyAsync(x=>x.UserName==username))
return true;
return false;
}
}

FeignClient throws instead of returning ResponseEntity with error http status

As I'm using ResponseEntity<T> as return value for my FeignClient method, I was expecting it to return a ResponseEntity with 400 status if it's what the server returns. But instead it throws a FeignException.
How can I get a proper ResponseEntity instead of an Exception from FeignClient ?
Here is my FeignClient:
#FeignClient(value = "uaa", configuration = OauthFeignClient.Conf.class)
public interface OauthFeignClient {
#RequestMapping(
value = "/oauth/token",
method = RequestMethod.POST,
consumes = MULTIPART_FORM_DATA_VALUE,
produces = APPLICATION_JSON_VALUE)
ResponseEntity<OauthTokenResponse> token(Map<String, ?> formParams);
class Conf {
#Value("${oauth.client.password}")
String oauthClientPassword;
#Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
#Bean
public Contract feignContract() {
return new SpringMvcContract();
}
#Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("web-client", oauthClientPassword);
}
}
}
and here how I use it:
#PostMapping("/login")
public ResponseEntity<LoginTokenPair> getTokens(#RequestBody #Valid LoginRequest userCredentials) {
Map<String, String> formData = new HashMap<>();
ResponseEntity<OauthTokenResponse> response = oauthFeignClient.token(formData);
//code never reached if contacted service returns a 400
...
}
By the way, solution I gave before works, but my initial intention is bad idea: an error is an error and should not be handled on nominal flow. Throwing an exception, like Feign does, and handling it with an #ExceptionHandler is a better way to go in Spring MVC world.
So two solutions:
add an #ExceptionHandler for FeignException
configure the FeignClient with an ErrorDecoder to translate the error in an Exception your business layer knows about (and already provide #ExceptionHandler for)
I prefer second solution because received error message structure is likely to change from a client to an other, so you can extract finer grained data from those error with a per-client error decoding.
FeignClient with conf (sorry for the noise introduced by feign-form)
#FeignClient(value = "uaa", configuration = OauthFeignClient.Config.class)
public interface OauthFeignClient {
#RequestMapping(
value = "/oauth/token",
method = RequestMethod.POST,
consumes = MULTIPART_FORM_DATA_VALUE,
produces = APPLICATION_JSON_VALUE)
DefaultOAuth2AccessToken token(Map<String, ?> formParams);
#Configuration
class Config {
#Value("${oauth.client.password}")
String oauthClientPassword;
#Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
#Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
#Bean
public Decoder springDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
}
#Bean
public Contract feignContract() {
return new SpringMvcContract();
}
#Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("web-client", oauthClientPassword);
}
#Bean
public ErrorDecoder uaaErrorDecoder(Decoder decoder) {
return (methodKey, response) -> {
try {
OAuth2Exception uaaException = (OAuth2Exception) decoder.decode(response, OAuth2Exception.class);
return new SroException(
uaaException.getHttpErrorCode(),
uaaException.getOAuth2ErrorCode(),
Arrays.asList(uaaException.getSummary()));
} catch (Exception e) {
return new SroException(
response.status(),
"Authorization server responded with " + response.status() + " but failed to parse error payload",
Arrays.asList(e.getMessage()));
}
};
}
}
}
Common business exception
public class SroException extends RuntimeException implements Serializable {
public final int status;
public final List<String> errors;
public SroException(final int status, final String message, final Collection<String> errors) {
super(message);
this.status = status;
this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SroException)) return false;
SroException sroException = (SroException) o;
return status == sroException.status &&
Objects.equals(super.getMessage(), sroException.getMessage()) &&
Objects.equals(errors, sroException.errors);
}
#Override
public int hashCode() {
return Objects.hash(status, super.getMessage(), errors);
}
}
Error handler (extracted from a ResponseEntityExceptionHandler extension)
#ExceptionHandler({SroException.class})
public ResponseEntity<Object> handleSroException(SroException ex) {
return new SroError(ex).toResponse();
}
Error response DTO
#XmlRootElement
public class SroError implements Serializable {
public final int status;
public final String message;
public final List<String> errors;
public SroError(final int status, final String message, final Collection<String> errors) {
this.status = status;
this.message = message;
this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
}
public SroError(final SroException e) {
this.status = e.status;
this.message = e.getMessage();
this.errors = Collections.unmodifiableList(e.errors);
}
protected SroError() {
this.status = -1;
this.message = null;
this.errors = null;
}
public ResponseEntity<Object> toResponse() {
return new ResponseEntity(this, HttpStatus.valueOf(this.status));
}
public ResponseEntity<Object> toResponse(HttpHeaders headers) {
return new ResponseEntity(this, headers, HttpStatus.valueOf(this.status));
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SroError)) return false;
SroError sroException = (SroError) o;
return status == sroException.status &&
Objects.equals(message, sroException.message) &&
Objects.equals(errors, sroException.errors);
}
#Override
public int hashCode() {
return Objects.hash(status, message, errors);
}
}
Feign client usage notice how errors are transparently handled (no try / catch) thanks to #ControllerAdvice & #ExceptionHandler({SroException.class})
#RestController
#RequestMapping("/uaa")
public class AuthenticationController {
private static final BearerToken REVOCATION_TOKEN = new BearerToken("", 0L);
private final OauthFeignClient oauthFeignClient;
private final int refreshTokenValidity;
#Autowired
public AuthenticationController(
OauthFeignClient oauthFeignClient,
#Value("${oauth.ttl.refresh-token}") int refreshTokenValidity) {
this.oauthFeignClient = oauthFeignClient;
this.refreshTokenValidity = refreshTokenValidity;
}
#PostMapping("/login")
public ResponseEntity<LoginTokenPair> getTokens(#RequestBody #Valid LoginRequest userCredentials) {
Map<String, String> formData = new HashMap<>();
formData.put("grant_type", "password");
formData.put("client_id", "web-client");
formData.put("username", userCredentials.username);
formData.put("password", userCredentials.password);
formData.put("scope", "openid");
DefaultOAuth2AccessToken response = oauthFeignClient.token(formData);
return ResponseEntity.ok(new LoginTokenPair(
new BearerToken(response.getValue(), response.getExpiresIn()),
new BearerToken(response.getRefreshToken().getValue(), refreshTokenValidity)));
}
#PostMapping("/logout")
public ResponseEntity<LoginTokenPair> revokeTokens() {
return ResponseEntity
.ok(new LoginTokenPair(REVOCATION_TOKEN, REVOCATION_TOKEN));
}
#PostMapping("/refresh")
public ResponseEntity<BearerToken> refreshToken(#RequestHeader("refresh_token") String refresh_token) {
Map<String, String> formData = new HashMap<>();
formData.put("grant_type", "refresh_token");
formData.put("client_id", "web-client");
formData.put("refresh_token", refresh_token);
formData.put("scope", "openid");
DefaultOAuth2AccessToken response = oauthFeignClient.token(formData);
return ResponseEntity.ok(new BearerToken(response.getValue(), response.getExpiresIn()));
}
}
So, looking at source code, it seams that only solution is actually using feign.Response as return type for FeignClient methods and hand decoding the body with something like new ObjectMapper().readValue(response.body().asReader(), clazz) (with a guard on 2xx status of course because for error statuses, it's very likely that body is an error description and not a valid payload ;).
This makes possible to extract and forward status, header, body, etc. even if status is not in 2xx range.
Edit:
Here is a way to forward status, headers and mapped JSON body (if possible):
public static class JsonFeignResponseHelper {
private final ObjectMapper json = new ObjectMapper();
public <T> Optional<T> decode(Response response, Class<T> clazz) {
if(response.status() >= 200 && response.status() < 300) {
try {
return Optional.of(json.readValue(response.body().asReader(), clazz));
} catch(IOException e) {
return Optional.empty();
}
} else {
return Optional.empty();
}
}
public <T, U> ResponseEntity<U> toResponseEntity(Response response, Class<T> clazz, Function<? super T, ? extends U> mapper) {
Optional<U> payload = decode(response, clazz).map(mapper);
return new ResponseEntity(
payload.orElse(null),//didn't find a way to feed body with original content if payload is empty
convertHeaders(response.headers()),
HttpStatus.valueOf(response.status()));
}
public MultiValueMap<String, String> convertHeaders(Map<String, Collection<String>> responseHeaders) {
MultiValueMap<String, String> responseEntityHeaders = new LinkedMultiValueMap<>();
responseHeaders.entrySet().stream().forEach(e ->
responseEntityHeaders.put(e.getKey(), new ArrayList<>(e.getValue())));
return responseEntityHeaders;
}
}
that can be used as follow:
#PostMapping("/login")
public ResponseEntity<LoginTokenPair> getTokens(#RequestBody #Valid LoginRequest userCredentials) throws IOException {
Response response = oauthFeignClient.token();
return feignHelper.toResponseEntity(
response,
OauthTokenResponse.class,
oauthTokenResponse -> new LoginTokenPair(
new BearerToken(oauthTokenResponse.access_token, oauthTokenResponse.expires_in),
new BearerToken(oauthTokenResponse.refresh_token, refreshTokenValidity)));
}
This saves headers and status code, but error message is lost :/

Call WCF Restful POST Method in MVC 5

I have to create simple WCF web service with GET and POST. See bellow source code
public interface ISample
{
[OperationContract]
[WebGet(UriTemplate = "/GetDEPT", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
Task<IEnumerable<DEPT>> GetDEPT();
[OperationContract]
[WebInvoke(UriTemplate = "UpdateDEPT?Id={Id}&StatusId={StatusId}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Task<bool> UpdateDEPT(List<DEPT> DEPT, string Id, string StatusId);
}
ISample interface Implementation : Sample
public class Sample: ISample
{
public async Task<IEnumerable<DEPTt>> GetDEPT()
{
return await DEPTBO.GetDEPT();
}
public async Task<bool> UpdateDEPT(List<DEPTt> DEPT, string Id, string StatusId)
{
return await DEPTBO.UpdateDEPTAsync(Id, DEPT, StatusId);
}
}
How to call this WCF Restful service in MVC 5?
Please help me Service integration in MVC Application
Now i found the solution for my question.
I have create class for proxy
namespace WCF.WCFService
{
public static class WebService<T> where T : class
{
public static string appSettings = ConfigurationManager.AppSettings["ServiceURL"];
public static IEnumerable<T> GetDataFromService(string Method, string param = "")
{
var client = new WebClient();
var data = client.DownloadData(appSettings + Method + param);
var stream = new System.IO.MemoryStream(data);
var obj = new DataContractJsonSerializer(typeof(IEnumerable<T>));
var result = obj.ReadObject(stream);
IEnumerable<T> Ts = (IEnumerable<T>)result;
return Ts;
}
}
public static class WebServiceUpdate
{
public static string appSettings = ConfigurationManager.AppSettings["ServiceURL"];
public static bool GetDataFromService_Update(string Method, List<CNHDataModel.CustomEntities.Port> portData, string param = "")
{
bool _res = false;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<CNHDataModel.CustomEntities.Port>));
MemoryStream mem = new MemoryStream();
serializer.WriteObject(mem, portData);
string data =
Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString(appSettings + Method + param, "POST", data);
_res = true;
bool Ts = (bool)_res;
return Ts;
}
}
}
Bellow, call the service proxy from controller
public class DEPTController : Controller
{
[ActionName("DEPTView")]
public ActionResult DEPTViewAsync()
{
try
{
IEnumerable<DEPT> DEPT = CNHService.WebService<DEPT>.GetDataFromService("GetDEPT");
if (port == null)
{
return HttpNotFound();
}
IEnumerable<Status> Status = CNHService.WebService<Status>.GetDataFromService("GetStatusAsync");
if (port == null || Status == null)
{
return HttpNotFound();
}
}
catch (Exception ex)
{
}
return View();
}
[HttpPost]
[ActionName("DEPTView")]
public ActionResult DEPTViewAsync([Bind(Include = "id,Statusid")] DEPT DEPTMENT)
{
try
{
List<DEPT> objDEPT = Session["DEPTItems"] as List<DEPT>;
List<DEPTStatus> objStatus = Session["DEPTIStatus"] as List<PortStatus>;
ViewBag.DEPTList = new SelectList(objDEPTt, "id", "Name");
ViewBag.DEPTStatusList = new SelectList(objStatus, "id", "Name");
if (ModelState.IsValid)
{
WebServiceUpdate.GetDataFromService_Update("UpdateDEPT", objDEPT, "?Id=" + DEPTMENT.Id + "&StatusId=" + DEPTMENT.Statusid);
setting.Message = true;
}
else
{
return View(setting);
}
}
catch (Exception ex)
{
}
return View(setting);
}
}
I hope this code help to WCF Restful service integration in MVC 5

Web API Filter stopped other filters from being hit

When I add the HttpHeader filter, the other filters stop working. Im not sure why.
BaseApiController.cs
[ExceptionHandling, ApiValidation, HttpHeader("X-Robots-Tag", "noindex, nofollow")]
public abstract class BaseApiController : System.Web.Http.ApiController
{
HttpHeaderFilter.cs
namespace Tournaments.Models.Mvc.Filters
{
public class HttpHeaderAttribute : System.Web.Http.Filters.FilterAttribute
{
public string Name { get; set; }
public string Value { get; set; }
public HttpHeaderAttribute(string name, string value)
{
Name = name;
Value = value;
}
public override bool AllowMultiple
{
get { return true; }
}
}
public class HttpHeaderFilter : System.Web.Http.Filters.IActionFilter
{
private readonly string _name;
private readonly string _value;
public HttpHeaderFilter(string name, string value)
{
_name = name;
_value = value;
}
public bool AllowMultiple
{
get { return true; }
}
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
return continuation().ContinueWith<HttpResponseMessage>((tsk) =>
{
HttpResponseMessage response = tsk.Result;
response.Headers.Add(_name, _value);
return response;
}, TaskContinuationOptions.OnlyOnRanToCompletion);;
}
}
}
Global.asax
protected override IKernel CreateKernel()
{
return Bootstrap.Configure((kernel) =>
{
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();
//kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InScope(q => HttpContext.Current ?? StandardScopeCallbacks.Thread(q));
kernel.Bind<IEmailService>().To<EmailService>().WithConstructorArgument("templates", emailTemplates);
kernel.Bind<IPaymentMethod>().To<AuthorizeNetProvider>().Named("AuthorizeNet");
kernel.Bind<IPaymentMethod>().To<StripeProvider>().Named("Stripe");
kernel.Bind<List<IPaymentMethod>>().ToConstant(kernel.GetAll<IPaymentMethod>().ToList());
ServiceBindings.Register(kernel);
RepositoryBindings.Register(kernel);
ValidationBindings.Register(kernel);
kernel.BindHttpFilter<ApiValidationFilter>(System.Web.Http.Filters.FilterScope.Action)
.WhenControllerHas<ApiValidationAttribute>();
kernel.BindHttpFilter<HttpHeaderFilter>(System.Web.Http.Filters.FilterScope.Controller)
.WhenControllerHas<HttpHeaderAttribute>()
.WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("name", q => q.Name)
.WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("value", q => q.Value);
GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlingAttribute());
GlobalConfiguration.Configuration.DependencyResolver = new Common.Ioc.NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.Formatters.Add(new CustomXmlFormatter());
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
});
}
I added this
TaskContinuationOptions.ExecuteSynchronously
Instead of
TaskContinuationOptions.OnlyOnRanToCompletion
and it works fine now.

Resources