Request Restsharp

Asked

Viewed 54 times

0

Good night people. I’m new to the C# world and I’m having the following problem using a C# code that makes an authentication request using Restsharp. This code was generated/exported from a request in Postman, so I know the request works.

It’s not a local url, it’s a login request in my production environment, I just changed the parameters, url, user and password.

using System;
using RestSharp;

                    
public class Program
{   
    public static void Main()
    {
        
        var client = new RestClient("https://minhaurldelogin/api/v3/login/");
        var request = new RestRequest(Method.POST);

        request.AddHeader("content-type", "application/json");
        request.AddParameter("application/json", "{\n  \"email\": \"[email protected]\",\n  \"senha\": \"M1NH4C3nha\"\n}", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        
        Console.WriteLine(response.ErrorException);
    }
}

Printing the response.ErrorException I have following mistake

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Net.HttpWebRequest.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback value)
   at RestSharp.Http.ConfigureWebRequest(String method, Uri url)
   at RestSharp.Http.ExecuteRequest(String httpMethod, Action`1 prepareRequest)
   at RestSharp.Http.PostPutInternal(String method)
   at RestSharp.Http.AsPost(String httpMethod)
   at RestSharp.RestClient.DoExecuteAsPost(IHttp http, String method)
   at RestSharp.RestClient.Execute(IRestRequest request, String httpMethod, Func`3 getResponse)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.SecurityPermission
The Zone of the assembly that failed was:
MyComputer

I don’t really know what I’m doing wrong. I appreciate the help.

1 answer

1


This has happened to me, this mistake is not very clear, but the part that helps the most is this:

System.Security.Permissions.Securitypermission The Zone of the Assembly that failed was: Mycomputer

In other words, you have a problem allowing access to an internet zone. Can solve this through configuration, in my case was adding a configuration to web.config. Your application seems to be a "Console Application", which may be slightly different, but in my case it was solved by adding to web.config:

<system.web>
 <trust level="Full" />  <- esta linha aqui
</system.web>

Note that I put the <system.web only for reference to identify where should be added, since the <trust> is a tag that goes from the previous.

If your code does not work because the config is different, you may have to add an attribute in your class or in the "Program" file, which by the error message must be in the namespace System.Security.Permissions.PermissionSet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.