1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Access services from a console application

Access services from a console application

1 posts, 0 answered
  1. David
    David avatar
    0 posts
    Registered:
    18 Sep 2016
    11 Dec 2016
    Link to this post

    Hello,

    I build a console application to make a synchronization between several xml files provided by an external source and some custom content types inside Sitefinity. This application call several services builded with Servicestack and hosted by the Sitefinity application.

    To access these services, the console application log a user with this piece of code:

    public static NameValueCollection SignIn(string siteUrl, string username, string password)
    {
        var formData = String.Format(SignInQueryStringFormat, username, password);
        var bytes = Encoding.UTF8.GetBytes(formData);
     
        WebRequest signInRequest = WebRequest.Create(AuthenticationHelper.GetSignInUrl(siteUrl));
        signInRequest.Method = "POST";
        signInRequest.ContentType = FormDataContentType;
        signInRequest.ContentLength = bytes.Length;
     
        Stream dataStream = signInRequest.GetRequestStream();
        dataStream.Write(bytes, 0, bytes.Length);
        dataStream.Close();
     
        Stream objStream = signInRequest.GetResponse().GetResponseStream();
     
        StreamReader objReader = new StreamReader(objStream);
     
        var token = objReader.ReadToEnd();
     
        var nameValueColl = HttpUtility.ParseQueryString(token);
        var bootstrapToken = nameValueColl[AuthenticationHelper.SwtWrapParam];
     
        var requestHeaders = new NameValueCollection();
        string tokenContent = string.Format(AuthenticationHelper.AuthorizationHeaderFormat, bootstrapToken);
        requestHeaders.Add(AuthenticationHelper.AuthorizationHeaderParameterName, tokenContent);
        return requestHeaders;
    }

     

    The request header generate by the signin method is put in the header of the request to call the Services.

    After that I receive a 401 error Unauthorized
    With the message : Missing configuration of security for the issuer tokens 'http://localhost:7987/SiteFinityWebApp/Sitefinity/Authenticate/SWT'

    So I tried to put a SecurityTokenIssuers in the backend with the following url:
    Http://localhost:7987/SiteFinityWebApp/Sitefinity/Authenticate/SWT

    After that another error was thrown : 401: Unauthorized | TokenExpired

    I don't know what I'm doing it wrong?

    Could someone help me?

    Thanks in advance.

    David.

1 posts, 0 answered