Inhalte aufrufen

Profilbild

Call SmartStore Web API from HTML(5) with Jquery(some library)

Web API HTML5 JQuery

  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
7 Antworten zu diesem Thema

#1 Jefferson

Jefferson

    Advanced Member

  • Members
  • 30 Beiträge

Geschrieben: 11 June 2014 - 14:08

Hello everyone,

 

it's my first post here and i want to be more clear as possible (english is not my mother tongue   :)

 

I would like to clear a doubt,about the web api concepts, we traffic Json and Json can be consumed by any device, isn't?

So there's any way to call the Web API service's using HTML(5) and some JQuery Library with security?

If exist's could someone share an short example,here or if the source code it's on githut or blog,etc?
Actually i'm trouble in the HTTP Header authetication part ,that must be provided some required data's.

 

If i can explain better, please let me know.

 

Thank's

 

Jefferson



#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3367 Beiträge

Geschrieben: 11 June 2014 - 14:48

Yes, that's possible. Go to the Codeplex download page and download "SmartStoreNET.WebApi.Client.Tools.zip".
The folder "JavaScript" contains a full API client sample using javascript including how to meet security requirements.

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#3 Jefferson

Jefferson

    Advanced Member

  • Members
  • 30 Beiträge

Geschrieben: 11 June 2014 - 15:16

Thank you for the quickly answer Marcus.

 

I gonna try and after i come here to give my feedback.



#4 Jefferson

Jefferson

    Advanced Member

  • Members
  • 30 Beiträge

Geschrieben: 12 June 2014 - 04:37

Actually i'm testing in my local machine using the javascript client test.

 

I create a new user and gave him permissions and active the API creating the keys.

 

But i'm getting  an problem with the call in the server side.

 

"Server cannot modify cookies after HTTP headers have been sent"

 

on the method below:

 
protected void SetCustomerCookie(Guid customerGuid)
        {
            if (_httpContext != null && _httpContext.Response != null)
            {
                var cookie = new HttpCookie(CustomerCookieName);
                cookie.HttpOnly = true;
                cookie.Value = customerGuid.ToString();
                if (customerGuid == Guid.Empty)
                {
                    cookie.Expires = DateTime.Now.AddMonths(-1);
                }
                else
                {
                    int cookieExpires = 24 * 365; //TODO make configurable
                    cookie.Expires = DateTime.Now.AddHours(cookieExpires);
                }
 
                _httpContext.Response.Cookies.Remove(CustomerCookieName);      // RIght here,in this line.
                _httpContext.Response.Cookies.Add(cookie);
            }
        }
 
It sounds like the response stream has already been partially written to and then the thread is trying to add the cookie.
 
Thanks


#5 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3367 Beiträge

Geschrieben: 12 June 2014 - 10:28

I cannot reproduce that. Any log entry available? Can you debug
SmartStore.Web.Framework\WebApi\Security\WebApiAuthenticateAttribute.cs::OnAuthorization?

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#6 Jefferson

Jefferson

    Advanced Member

  • Members
  • 30 Beiträge

Geschrieben: 12 June 2014 - 16:27

Thanks for the hint.

 

Yes, i can debbug and right there is the issue when we try to set the customer to the customer workContext.

 

Method : public override void OnAuthorization(HttpActionContext actionContext)

 

line: 206
if (result == HmacResult.Success)
{
_workContext.CurrentCustomer = customer;  // Here is the problem, when, we try to set the customer to the _workContext.
 
var response = HttpContext.Current.Response;
 
response.AddHeader(WebApiGlobal.Header.Version, cacheControllingData.Version);
response.AddHeader(WebApiGlobal.Header.MaxTop, WebApiGlobal.MaxTop.ToString());
response.AddHeader(WebApiGlobal.Header.Date, now.ToString("o"));
 
response.Cache.SetCacheability(HttpCacheability.NoCache);
}


#7 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3367 Beiträge

Geschrieben: 12 June 2014 - 20:48

Wrap up
_httpContext.Response.Cookies.Remove(CustomerCookieName);
like this
try
{
   _httpContext.Response.Cookies.Remove(CustomerCookieName);
}
catch (Exception) { }
 
and test if it solves it for you.

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#8 Jefferson

Jefferson

    Advanced Member

  • Members
  • 30 Beiträge

Geschrieben: 13 June 2014 - 01:34

Actually it's not solve.

 

But comment the line as described upside it's okay.

 

Thanks for the support Marcus




Auch markiert mit einem oder mehrerer dieser Schlüsselwörter: Web API, HTML5, JQuery