Rest Media Files

Ivan Louw asked on May 28, 2019 04:58

Hi, I am trying to upload a file using the rest API for kentico 12 MVC. This is not a physical, just adding the meta data to sql.

I have the following Localhost/rest/media.file/site/Website?FileName=test2&FileTitle=aaa&FileDescription=aaa&FileExtension=.txt&FileMimeType=image/jpeg&FilePath=test2.jpg&FileSize=55&FileLibraryID=1&FileSiteID=1

Currently I am getting the following error: 404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

Any help will be appreciated.

Correct Answer

Ivan Louw answered on June 12, 2019 06:41

Hi everyone, Got it to work. I had to set the .Net Authorization Rules at site level to allow all users.

Thanks for all the help and advice.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Ivan Louw answered on May 28, 2019 06:52 (last edited on May 28, 2019 06:54)

Update

I also tried using a program using RestSharp to consume the service, but I am not getting authorized

public partial class WebForm1 : System.Web.UI.Page
{
    string url = "https://localhost/Kentico/rest";
    protected void Page_Load(object sender, EventArgs e)
    {


        var client = new RestClient(url);
        //client.Authenticator = new HttpBasicAuthenticator("ServiceUser", "password");
        var request = new RestRequest("/content/currentsite/en-us/all/news?format=json?hash=566ff4b4364467cb10983a23fd1a4801e8322b62675f00cc6c94efc8716342c6", Method.GET);
        //request.AddHeader("Authorization", "Basic 566ff4b4364467cb10983a23fd1a4801e8322b62675f00cc6c94efc8716342c6"); // + Base64Encode("ServiceUser:password"));

        IRestResponse response = client.Execute(request);

        var content = response.Content;

    }

    public static string Base64Encode(string plainText)
    {
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
        return System.Convert.ToBase64String(plainTextBytes);
    }
}

}

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on May 28, 2019 08:31

Ivan,

I'm not sure how RestSharp works, but this is how I used to consume Kentico REST service with regular .NET: http://bitsorchestra.com/Idea/August-2015/Consume-REST-service-on-the-server

I don't think Kentico REST works with media files (just pages or objects), so I'm afraid /rest/media.file/site/Website?... URL is not correct one.

Also I'd recommend you to check permissions for the user you're trying to authenticate with - it might be the case where you're getting authenticated, but user is not authorized to access the object you're requesting.

Hope this helps

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 28, 2019 09:00

Make sure you have followed the documentation closely and added also the runAllManagedModulesForAllRequests="true" attribute to the web.config file.
Then, I was able to "register" the media file in the system using this REST API call:
http://mydomain.com/rest/media.file?format=json

and the POST request body was:

{  
"FileDescription":"Filedescription",  
"FileExtension":".pdf",  
"FileLibraryID":1,  
"FileMimeType":"application/pdf",  
"FileName":"filename",  
"FilePath":"some/path/file.pdf",  
"FileSiteID":1,  
"FileSize":1024,  
"FileTitle":"filename.pdf"  
}  

Keep in mind that this just registers the file in the Kentico DB. You need to place the physical file, the binary data to given disk location using FTP or some file system sync software.

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on May 28, 2019 09:45

Hi thanks for the feedback.

Roman I tried your code and I still not getting authenticated. I have created a separate user account but we use Active directory for authentication, maybe this is why i am getting errors. Will setup forms or mixed mode to see if I am still getting not authenticated.

We also running 2 site, one for admin and the site and I have only applied the config values to the admin site. I guess this is the correct place.

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on May 28, 2019 09:49

Hi Juraj,

I have checked the documentation and the config and everything seems to be in order.

The user I am testing with is an administrator on the site. So he should have access to everything.

The files are ftp'd and the I must use this service to perform crud operation on the meta data of the file , which is stored in sql.

I am aware that you cannot upload the physical file,

thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 28, 2019 10:34

I would try using REST API using PostMan tool - just to be sure Kentico is configured correctly, that the user account is able to access given object type and create its record. If yes, then the issue will be in the code you are using. Maybe the authentication hash is not correct.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 28, 2019 17:33

Hi Ivan,

I've performed a few tests myself and found that 404 page is being returned only if HTTP Activation is not enabled as it is described here in documentation.

Please make sure you have HTTP Activation enabled for .NET Framework

Image Text

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on May 30, 2019 07:15

Hi everyone, I changed my site back to forms authentication. And when I try to browse the rest api, I get a windows authentication challenge? https://localhost/rest/ Is this not maybe why I can not authenticate as the site is setup as per kentico documentation and I have double checked all the config and suggestions mentioned in previous posts.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 30, 2019 07:19

I think I got it now - you cannot use Windows authentication with REST. You need to enable the Anonymous authentication just like described in the docs and then, to authenticate the REST call you need to use the authentication header. This means, you need to have user account which is not domain user in Kentico and use this user account to authenticate the REST calls. Please see Authenticating rest requests for more details.

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on May 30, 2019 07:53

Hi Juraj,

My website only uses forms and Anonymous authentications. When I navigate to the url now, I get a forms authentication and then after that I get asked again with windows authentication. Although in both occasions I have to login using the credentials of the kentico user added to get to the service.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 30, 2019 08:00

I am sorry but I am confused, you said " I changed my site back to forms authentication" so I thought it was set to Windows authentication - I mean Kentico was set to use Windows authentication.
Could you please describe how the IIS is configured? For example in the Authentication setup I have Anonymous and Forms authentication enabled, all other are disabled. Then, I have created a dummy user account in Kentico, set it to have admin privilege level and also set some password. Then, when using PostMan to do the REST call, I am adding the Authorization header and filling out the user name and password of the dummy user.

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on May 30, 2019 08:46 (last edited on June 5, 2019 09:26)

Hi Juraj,

I changed back my site to windows authentication to forms. This was done in the following steps: Commented deny all users in the config. Then I changed

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on June 5, 2019 09:27 (last edited on June 5, 2019 09:27)

Hi Juraj,

I changed back my site to windows authentication to forms. This was done in the following steps: Commented deny users section in the web config. Then I changed authentication mode="Forms" from windows to forms I added a dummy user account and made the account an global administrator.

In IIS in the authentication app I enabled only Forms and Anonymous authentication. All other are disabled.

Yet when I navigate the rest service I have a popup requesting a login using kentico user details.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on June 5, 2019 12:26

Isn't it possible that the Windows authentication required for access to the live site and this is enabled in your web.config file:

<location path="">
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</location>
0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on June 8, 2019 13:29

Hi Jurai,

Thank for the help.

I removed that section and changed it to allow *.

Still no luck.

Ivan

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on June 10, 2019 07:47 (last edited on July 8, 2019 06:34)

Are you able to make REST calls using the PostMan tool, just for testing? Are you able to reproduce the issue on a clean Kentico instance? Have you tried commenting out that section so it is not applied at all?

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on June 21, 2019 01:40

Hi everyone, Thanks for all the help. I got this to work finally, I had to grant access to all at site level in IIS.

In the .Net Authorization rules added allow all users.

Thanks again.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.