Angular2 + Web API + token based authentication - asp.net-web-api

I am trying to learn Angular2
and I am trying to create a simple blog with authentication.
this here is my add a new post method:
[Authorize]
// POST: api/Post
public PostModel Post([FromBody]PostViewModel model)
{
var post = new PostModel
{
Body = model.Body,
Title = model.Title,
AuthorId = IdentityExtensions.GetUserId(User.Identity),
};
var res = blogRepo.AddPost(post);
return res;
}
everything works fine, but IdentityExtension.GetUserId() do not return the most current logged in user but the first user since the app started.
basically I am looking for a way to make sure that the current user logs out on the server as well as on the client (the client side is just a simple removal of the localStorage.removeItem("jwt");)
also there is a good chance that what I am doing is totally wrong, but I can't access the ApplicationUserManager in this controller.

ok I have found the problem, although I haven't managed to solve it yet but I will update this when i do, and I am writing this as an answer since the problem is totally different from what I asked and thought to be.
the problem is related to sending the authentication hints as Thierry Templier suggested. I have a file that exports headers like this:
export const authHeaders = new Headers();
authHeaders.append('Accept', 'application/json');
authHeaders.append('Content-Type', 'application/json');
authHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('jwt'));
And I Import this header where ever I need it. but I am not sure why it always sends a cached value (i.e the first value stored on the client and it has nothing to do with the server side as my question implies).
to solve this issue I just have to make sure the latest access-token stored on localstorage is sent to the server.
EDIT: for now I am constructing the headings in the constructor.

Related

ASP.NET Core API POST parameter is always null from Firefox

In my Angular2 Application, I'm submitting a form and send data through POST API to dotnet core backend. I've created a new form, that is working fine with chrome, but on firefox, I'm receiving null in POST API parameter.
I'm all stuck what to search and how to?? I've checked every possible issue and didn't find anything, because App is working fine with chrome, all data is up to date and correct but a single form is not working on firefox.
Can anyone help me out what to do? because I'm totally stuck and have
no idea what to do??
My Endpoints are;
[HttpPost]
[Route("api/Intimation/SaveIntimation")]
public async Task<ActionResult> SaveIntimation([FromBody] ViewModelCreateIntimation objCreateIntimation)
{
if (objCreateIntimation == null || objCreateIntimation.objIntimation == null)
{
return Ok("null received");
}
// remaining code
}
my service on angular side
saveIntimation(intiModel) {
console.log(intiModel);
return this.httpClient.post<ViewModelResponse>(this.baseUrl + this._SubmitIntimationUrl, JSON.stringify(intiModel), { headers: this.configurations.getHeaderWithAuth() });
}
where this._SubmitIntimationUrl is "/api/Intimation/SaveIntimation", intiModel is object that I'm passing.
Controller function - Angular
this.intimationModel = this.admissionForm.value;
this.adminService.SubmitAdmissionIntimationService(this.createIntimationModel).subscribe(
(response) => {
this.responseModel = response;
// further process
},
(error) => {
this.notification.onClear();
this.notification.onError(this.errorHandler.handleError(error).error, Constants.MESSAGE_ERROR);
}
);
Data that is sending from service (Last place where I can check data)
The problem looks like it's because of the name of the parameter in your controller is different to that being passed up in the request.
In your controller, the parameter the framework is trying to bind to is called objCreateIntimation, but your request shows you're sending up objIntimation instead. As they have different names, the model binder has no idea that objIntimation should be bound to objCreateIntimation.
Give them both the same name, and that should fix it for you.
I went through same issue once, and It took almost a day to figure out the reason behind it,
do check your date pickers and its values, and make sure it is not null and its format is also correct. Because firefox is a bit strict in this matter and a litter change in datepicker makes it null.
hope it helps you.

"the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request."

I'm trying to implement ajax pagination using Umbraco.
On the server side, I have the following:
[System.Web.Http.HttpGet]
public JsonResult pagination(int? page)
{
IEnumerable<IPublishedContent> newsPosts = Umbraco.AssignedContentItem.DescendantOrSelf("news").Children.Where(x => x.IsVisible() && x.DocumentTypeAlias == "newsPost").OrderByDescending(x => x.UpdateDate).Take(5);
//from here on we will be returning the json within which information required for displaying post entries in carousel is included.
string json = "[some random string]"; //just random string for now.
return Json(json, JsonRequestBehavior.AllowGet);
}
As you can see, I'm trying to get necessary data from IPublishedContents, but I'm having trouble instantiating this series of IPublishedContents.
And this is the error I'm getting when I access:
locahost:{port}/umbraco/surface/{controller}/pagination on Chrome.
Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request.
Details: System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request.
As I said, I'm making this request from Chrome, which is I think means this request is from the front end, so I'm not sure why I'm getting this error.
In the course of searching I found these
1) our.umbraco.com forum
2) stackoverflow post
is deserted with no answer, and as for 2, it strikes me that the answer is not quite relevant to my case. I want to instantiate IPublishedContent in the first place.
Mine is Umbraco 7.
and could it be possible to tell me why requests from the front-end are not desirable?
Any hint would be highly appreciated.
Thanks,
Try getting your node this way.
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var yourNode = umbracoHelper.TypedContentAtXPath("umbracoPathtoYourNode");
Perhaps easier to use web api
Create a controller which inherits from UmbracoApiController
public class PagedItemsController : UmbracoApiController
{
[HttpGet]
[ActionName("list")] //Optional see note below
public IHttpActionResult GetItems([FromUri] int pageNo = 1)
{
// Next you need some way of getting the items you need.
// I would not return the whole IPublishedContent items. Rather query those and then use linq Select to transform into a more relevant smaller class (not doing this here)
// I've just included this for brevity
var items = _itemService.GetPagedItems(pageNo);
// Now return the results
return Ok(items);
}
}
Calls to endpoints in Umbraco follow the format
/umbraco/api/{controller}/{endpoint}
With the [ActionName("list")] above the call to the GetItems method will be
http://example.com/umbraco/api/PagedItems/list?pageNo=3
Without the ActionName attribute the call would be
http://exampe.com/umbraco/api/PagedItems/GetItems?pageNo=3
With a standard jquery ajax call this will return json without needing to serialise.

Django REST framework FileField PUT Testcase

I have a model that contains a FileField which may not be blank. When creating tests for this model, I've run into the problem that I get errors when testing with PUT, while the exact same thing works when doing a POST.
As views I'm simply using generics.ListCreateAPIView for the POST destination and generics.RetrieveUpdateDestroyAPIView for the PUT destination, both work normally when using the API in browser.
The payload for the POST and PUT is created as follows:
uploaded_file = SimpleUploadedFile('TestCode4.c', "Testcode", content_type='text/plain')
self.valid_payload = {
'name': 'TestValid',
'test_file': uploaded_file
}
Then the working POST test looks as follows:
client = Client()
response = client.post(
reverse('code-list'),
self.valid_payload,
format='json'
)
And the PUT:
client = Client()
response = client.put(
reverse('code-detail', kwargs={'pk': 1}),
self.valid_payload,
format='json'
)
The POST returns 204 and creates a new object, while the PUT returns 415 with the following error:
{u'detail': u'Unsupported media type "application/octet-stream" in request.'}
I am unsure what is going wrong here, it seems that both the post and put are passing the SimpleUploadedFile data in the same way, though with put it somehow becomes an octet stream.
I figured out the problem Django's django.test.Client class does not support the 'PUT' method. Instead the REST framework provides the class rest_framework.test.APIClient, which does support PUT (and PATCH, etc).
The client.put() function now needs to be filled in a little differently (I was unable to get it to work with SimpleUploadedFile) as explained here: https://fodra.github.io/2017/05/31/testing-django-rest-api-with-image-field.html

What is the proper way to update attribute value in started Spring WebSession?

I have SpringBoot 2.0.0.M7 project where I am using WebSession with Redis (org.springframework.session:spring-session-data-redis:2.0.0.RELEASE).
I have a WebFlux route which supposed to do redirect to eBay with the eBay session id. Every time user is visiting that route I need to request different session id from eBay API and include it into the redirect URL. Later, eBay will redirect user back to my application where I need that session id to request token.
During testing I saw that that value of the session attribute (in my case it's ebay_session_id) can't be replaced with the new value when browser still have a cookie with the existing session ID. In the route where I am requesting again ebay_session_id I am getting old value and not the new one.
The code which store SessionID is following:
return ebayApiReactiveWrapper
.getSessionId(apiContext)
.flatMap(sessionId ->
request
.session()
.map(webSession -> {
webSession
.getAttributes()
.put("ebay_session_id", sessionId);
return sessionId;
})
)
.flatMap(sessionId -> {
final UriBuilder uriBuilder = uriBuilderFactory.uriString(
ebayApiSettings.getSignInUrl()
);
uriBuilder.queryParam("runame", ebaySettings.getRuName());
uriBuilder.queryParam("SessID", sessionId);
return ServerResponse.temporaryRedirect(redirectUri).build();
});
I tried to add webSession.save() after put method but it doesn't help.
What I am doing wrong? Thank you in advance!
UPDATE
Some new details about what is happening with the session data in Redis. When session is created (empty Redis) the data looks like that:
127.0.0.1:6379> hkeys "spring:session:sessions:cbbf8000-6ce8-4238-a427-9aab37d2702b"
1) "lastAccessedTime"
2) "maxInactiveInterval"
3) "creationTime"
4) "sessionAttr:ebay_session_id"
When I visit same route second time (session cookie still exists and the session data is still in Redis) the data is changing:
127.0.0.1:6379> hkeys "spring:session:sessions:cbbf8000-6ce8-4238-a427-9aab37d2702b"
1) "sessionAttr:ebay_session_id"
Through, sessionAttr:ebay_session_id still contains value from the first request.
The worst thing is that such structure cause NullPointerException when another route is trying to get session data. Looks like it expecting other 3 fields to be presented and fails when it's not the case.
Looks like not many people faced such issue. I found my solution to solve it.
I saw that session will not be updated if the attribute set of the session is not changed. So I am adding a new attribute every time I need to update session value. My first flatMap from the question code is changed the following way:
.flatMap(sessionId ->
request
.session()
.map(webSession -> {
// we need to check if session already started before applying "hack"
if (webSession.isStarted()) {
// "hack"
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yMdkmsn");
webSession
.getAttributes()
.put(LocalDateTime.now().format(dateTimeFormatter),"1");
}
webSession
.getAttributes()
.put("ebay_session_id", sessionId);
return sessionId;
})
)
When new attribute is added session considered as changed and will be updated in redis.
I faced that problem and used almost the same hack, but just by updating a primitive typed session attribute.
For example:
webSession.getAttributes().put("userContext", userContext);
webSession.getAttributes().put("lastContextUpdateTime", System.currentTimeMillis());
This avoids to add a new attribute for each session update, but triggers the mutable object update in Redis too.

Ember Data update POST when it should be PUT

I'm working on an Ember.js app. I have an update function, part of an ObjectController.
The function should save my updated model, however when I call save(); it sends a POST request not a PUT request. (Tested in Chrome.)
Why would that happen? How can I make sure a PUT request is sent for updates?
Here is my code:
customer = this.get('model');
customer.set('name', 'New name');
customer.save();
For extra reference, when I log the "dirtyType" with console.log( customer.get('dirtyType') ); it says "updated".
Any help very much appreciated!
UPDATE
I've adjusted the sample code above to make it clearer, I am NOT creating a new model and wanting to use PUT. I have an existing model that I need to update.
I'm not sure if your workaround is correct in the land of PUT vs POST.
TL;DR PUT should define the resource (by Request-URI), but we don't do that during creation, so we shouldn't be using a POST. Override the create/save if you need this for your server, instead of hacking the isNew property, which may come back to bite you.
Put
9.6 PUT
The PUT method requests that the enclosed entity be stored under the
supplied Request-URI. If the Request-URI refers to an already
existing resource, the enclosed entity SHOULD be considered as a
modified version of the one residing on the origin server. If the
Request-URI does not point to an existing resource, and that URI is
capable of being defined as a new resource by the requesting user
agent, the origin server can create the resource with that URI. If a
new resource is created, the origin server MUST inform the user agent
via the 201 (Created) response. If an existing resource is modified,
either the 200 (OK) or 204 (No Content) response codes SHOULD be sent
to indicate successful completion of the request. If the resource
could not be created or modified with the Request-URI, an appropriate
error response SHOULD be given that reflects the nature of the
problem. The recipient of the entity MUST NOT ignore any Content-*
(e.g. Content-Range) headers that it does not understand or implement
and MUST return a 501 (Not Implemented) response in such cases.
If the request passes through a cache and the Request-URI identifies
one or more currently cached entities, those entries SHOULD be
treated as stale. Responses to this method are not cacheable.
The fundamental difference between the POST and PUT requests is
reflected in the different meaning of the Request-URI. The URI in a
POST request identifies the resource that will handle the enclosed
entity. That resource might be a data-accepting process, a gateway to
some other protocol, or a separate entity that accepts annotations.
In contrast, the URI in a PUT request identifies the entity enclosed
with the request -- the user agent knows what URI is intended and the
server MUST NOT attempt to apply the request to some other resource.
If the server desires that the request be applied to a different URI,
Custom Adapter
App.ApplicationAdapter = DS.RESTAdapter.extend({
createRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record, { includeId: true });
//return this.ajax(this.buildURL(type.typeKey), "POST", { data: data });
return this.ajax(this.buildURL(type.typeKey), "PUT", { data: data });
},
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var id = get(record, 'id');
// you could do the same here, but it's even more incorrect
return this.ajax(this.buildURL(type.typeKey, id), "PUT", { data: data });
},
});
http://www.ietf.org/rfc/rfc2616.txt
Thank you for all of your help guys, however I have found the issue and it is ridiculously silly.
The API I have been using had a new flag "is_new" and that had been added to the model and was overwriting the "isNew" property.
Causing Ember (and me) to get very confused.
I've tweaked the API and all is good in the world!
If the model was created with createRecord, and thus has isNew == true and you call save() the expected behavior is POST. Once the record has been persisted, and it is changed, and thus isDirty == true but isNew == false then the save() will be a PUT.
This is described in the Models Guide.

Resources