Auth0 Home Blog Docs

Login using Custom UI in ASP.NET Core

custom-login
aspnet-core

#1

Hi All

I am using .NET core to try to login. I have the following code:

 public static bool Login(LoginViewModel vm, Auth0 auth0Options)
        {
            var client = new RestClient($"https://{auth0Options.Domain}/dbconnections/login/");
            var request = new RestRequest(Method.POST);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json",
                "{\"client_id\": \"" + auth0Options.ClientId +
                "\",\"tenant\": \"" + auth0Options.ClientId +
                "\",\"email\": \"" + vm.Email +
                "\",\"password\": \"" + vm.Password +
                "\",\"connection\": \"Username-Password-Authentication" +
                "\"}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.IsSuccessful;
        }

Instead of getting signed in, I get IsSuccessful = false. The response body is:

{
    "name": "BadRequestError",
    "code": "missing_property",
    "description": "tenant must be specified",
    "statusCode": 400
}

What am I doing wrong?

Thanks and Regards
Zawar


#2

Hello… does anyone answer questions around here? Anyone?


#4

:wave: @zawarq the clientid is different from your tenant name, and you havae auth0Options.ClientId for both client_id and tenant. ClientID is the application ID found here https://manage.auth0.com/#/applications and the tenant name is in the top right corner when you are in manage.auth0.com . The reason you are getting an error is because you did not put the correct tenant name in the parameters.


#5

Hi Kim

Thank you for your help :grin:. I modified the code to as follows:

            var client = new RestClient($"https://{auth0Options.Domain}/dbconnections/login/");
            var request = new RestRequest(Method.POST);
            request.AddHeader("content-type", "application/json");

            request.AddParameter("application/json",
                "{\"client_id\": \"" + auth0Options.ClientId +
                "\",\"tenant\": \"" + auth0Options.Tenant +
                "\",\"username\": \"" + vm.Email +
                "\",\"password\": \"" + vm.Password +
                "\",\"connection\": \"Username-Password-Authentication" +
                "\"}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.IsSuccessful;

and now I get this error:

{"name":"ValidationError","code":"invalid_user_password","description":"Wrong email or password.","statusCode":400}

However I am sure of the user name and password as I they work fine with the Lock widget. Note that I also tried to change “username” to “email” and to “username/email” in the .AddParameter call.

Kindly help.

Regards
Zawar


#6

Bump.

We really need working examples for .NET for login and sign up (I already got signout and password change working).