I need to test this mvc method from postman, it is working in Browser.
[HttpPost]
[Route("login")]
public async Task<ActionResult> Login(LogInRequest logInRequest)
{
User user = null;
if (ModelState.IsValid)
{
.....
This is LogInRequest Class:
public class LogInRequest
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
public string Token { get; set; }
public bool IsResetPassword { get; set; }
public bool IsThankYouPage { get; set; }
}
I am not sure how to sent the LogInRequest parameter using Postman. anything I try it is not hitting the breakpoints in first line of this method.
This is how I call the local URL and send parameter to this method.
I know this an old question but it may be helping the others.
You should use the Route which you provided in the controller e.g
[Route("login")]
Url in postman call should be like this
HTTP:/localhost:12xx3/login.
Related
I have a webapi controller with the following method signature:
public IHttpActionResult GetAttractions([FromUri] SearchAttractionRequest request)
The class SearchAttractionRequest looks like:
public class SearchAttractionRequest
{
public string Region { get; set; }
public string Category { get; set; }
public string Genre { get; set; }
}
This all works fine.
I am looking to create a new endpoint with same but this time the parameter will have fields that are guids so it will look like:
public class SearchAttractionGuidRequest
{
public Guid? Region { get; set; }
public Guid? Category { get; set; }
public Guid? Genre { get; set; }
}
And the new endpoint will be :
public IHttpActionResult GetAttractions([FromUri] SearchAttractionGuidRequest request)
Given that the parameters are coming from query string would the binding be able to bind? At the querystring level it's all strings. Ideally, I want to avoid creating a method with a new name.
Currently when i try this i get:
Multiple actions were found that match the request: GetAttractions
You can avoid creating a new method name by specifying different [Route] attributes for those methods.
Please any one help me out to design enquiry form that having Name,Mobile No,Query and Address attributes and all these attributes should be sended to my email when visitor clicked send button after fill same details.
Need code for MVC..
First you need to create a model something like this:
public class Inquiry
{
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public DateTime Date { get; set; }
public string Message { get; set; }
}
Then you need in your controller when returning your view to pass in a new model:
[HttpGet]
public IActionResult Contact()
{
return View(new Inquiry());
}
Then create your form in HTML
Then in your controller have:
[HttpPost]
public IActionResult Contact(Inquiry inquiry)
{
{new Email().Send(inquiry); //Using your email class
return View(<Thank you page or whatever>);
}
Don't forget validations etc...
I have the following complex model:
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public int UserId { get; set; }
}
I need to bind the whole model in my action method using [FromBody], while Id property should come [FromQuery]. My action method looks like this:
public IActionResult Delete([FromBody]User userRequest)
{
// Some code
}
The thing is that I can't change the model, because it is 3-rd party and also, I can't have Id as the second parameter in action method, because I have validation logic for userRequest where I need the Id. Any ideas?
Use a DTO/view model and map over to User. For example:
public class UserDTO
{
public string UserName { get; set; }
public int UserId { get; set; }
}
Then:
public IActionResult Delete(int id, [FromBody]UserDTO userRequest)
{
var user = new User
{
Id = id,
UserName = userRequest.UserName,
UserId = userRequest.UserId
}
// do something
}
I have a Web API POST method that excepts a custom complex object MyObjectRequest as a parameter and returns a custom complex object MyObjectResponse. The MyObjectResponse object has custom complex object Token as a property.
public class MyObjectRequest
{
public string AppName { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string AppIdentifier { get; set; }
}
public class MyObjectResponse
{
public bool Authenticated { get; set; }
public Token AccessToken { get; set; }
}
public class Token
{
public string Id { get; set; }
public string ExpirationDate { get; set; }
}
I have Web API controller where when the user makes a HTTP POST call, I want to return the MyObjectResponse.
public class MyCustomController : Controller
{
public MyObjectResponse Post([FromBody] MyObjectRequest request)
{
//do my work here
}
}
Is this the correct way of making my MyCustomController API signature?
What you have certainly can work. I tend to wrap those objects in the HttpResponseMessage like below:
[HttpPost]
public HttpResponseMessage Post([FromBody] MyObjectRequest request)
{
if (ModelState.IsValid) // and if you have any other checks
{
var myObjectResponse = new MyObjectResponse();
// In your case, this will be result of some service method. Then...
return Request.CreateResponse(HttpStatusCode.Created, myObjectResponse);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
[HttpPut]
public HttpResponseMessage Update([FromBody] UserModel userModel)
{
if (ModelState.IsValid)
{
var myObjectResponse = new MyObjectResponse();
// In your case, this will be result of some service method. Then...
return Request.CreateResponse(HttpStatusCode.Accepted);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
[HttpGet]
public HttpResponseMessage Get(int id)
{
var myObjectResponse = GetObjectFromDb(id);
// In your case, this will be result of some service method. Then...
if(myObjectResponse == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
return Request.CreateResponse(HttpStatusCode.OK, myObjectResponse);
}
This way the client can just look at the status code and decide what to do with the response without actually trying to deserialize it. You can get more info on HttpStatusCodes at this MSDN article.
They have added more methods such as ApiController.Ok in WebApi2. For more information, you can take a look at this ASP.NET WEB API overview page.
Yes, this is perfectly fine as an api signature
I've used the OAuth1Authenticator class from the Xamarin.Auth component library to allow users to login via Twitter. It authenticates correctly and gets me a response which has the following: oauth_token, oauth_token_secret, user_id, screen_name, oauth_consumer_key, oauth_consumer_secret.
Here is my code
OAuth1Authenticator twitterAuthenticator = new OAuth1Authenticator(Constants.Twitter.CONSUMER_KEY, Constants.Twitter.CONSUMER_SECRET, new Uri(Constants.Twitter.REQUEST_TOKEN_URL), new Uri(Constants.Twitter.AUTHORIZE_URL), new Uri(Constants.Twitter.ACCESS_TOKEN_URL), new Uri(Constants.Twitter.CALLBACK_URL));
twitterAuthenticator.Completed += async (sender, e) =>
{
if (!e.IsAuthenticated)
{
return;
}
string oauth_token = e.Account.Properties["oauth_token"].ToString();
The question is how do I then use that response to signup/signin a Parse User ? i.e. I want a ParseUser created on the parse database via the Twitter token and the session should be taken care of, same way it works for sign-via-Facebook using ParseFacebookUtils class
Problem is Parse doesn't support Login via Twitter in Xamarin, however, I believe parse does support any type of 3rd party authentication in an alternative way as shown below but I don't know how to do it.
Here are the most relative links
https://parse.com/tutorials/adding-third-party-authentication-to-your-web-app but the problem in this link is that it's made as a webpage button, don't know how to use that on a mobile, and it's for GitHub don't know how to use it for Twitter instead (Twitter is only OAuth1)
http://blog.parse.com/2013/12/03/bring-your-own-login/ This is exactly what I need but it needs a session Token, doesn't work with the oauth_tokens that twitter responds back to me, hence don't know how to use the method mentioned in the link
https://github.com/auth0/rules/blob/master/parse.md This looks like the solution, however I don't know how to use it, it does show the twitter icon so it should work with twtiter but how do I get that to work in .NET Update: I've found this xamarin component http://components.xamarin.com/view/Auth0Client which gets me closer to use the method mentioned in the first link in this paragraph, yet I'm still lost and don't know how to link autho0 to parse
All in all, I'm lost in this maze and really wish anyone could help me out.
I don't have a Twitter account so I can't test this but it looks like your POST DTO would be this:
public class TwitterAuthRequest
{
public AuthData authData { get; set; }
}
public class Twitter
{
public string id { get; set; }
public string screen_name { get; set; }
public string consumer_key { get; set; }
public string consumer_secret { get; set; }
public string auth_token { get; set; }
public string auth_token_secret { get; set; }
}
public class AuthData
{
public Twitter twitter { get; set; }
}
With a response DTO like this:
public class TwitterAuthResponse
{
public string username { get; set; }
public string createdAt { get; set; }
public string updatedAt { get; set; }
public string objectId { get; set; }
public string sessionToken { get; set; }
public AuthData authData { get; set; }
}
public class Twitter
{
public string id { get; set; }
public string screen_name { get; set; }
public string consumer_key { get; set; }
public string consumer_secret { get; set; }
public string auth_token { get; set; }
public string auth_token_secret { get; set; }
}
public class AuthData
{
public Twitter twitter { get; set; }
}
Don't forget to put in the headers:
("X-Parse-Application-Id", ApplicationId)
("X-Parse-REST-API-Key", ApplicationKey)
Source: https://parse.com/docs/rest#users
EDIT:
I have a created a draft for how you would use the DTO's:
public static class TwitterLoginProvider
{
public static Task<ServiceResponse<TwitterAuthResponse>> Login(
Twitter twitterInfo,
string applicationId,
string apiKey,
IRestClient restClient)
{
var request = new TwitterAuthRequest ()
{
authData = new AuthData ()
{
twitter = twitterInfo
}
};
restClient.AddHeader ("X-Parse-Application-Id", applicationId);
restClient.AddHeader ("X-Parse-REST-API-Key", apiKey);
return restClient.PostAsync<TwitterAuthResponse>("https://api.parse.com/1/users", request, Format.Json);
}
}
When you get the response from Xamarin.Auth, use that info to create a Twitter object and pass it to the IRestClient. As a response from the service you will get a response with the session information.
IRestClient interface: https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/IRestClient.cs
Sample implementation: https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/RestClient.cs
Alternatively you could use RestSharp: http://restsharp.org/