Hi @rueben.tiow
Thanks for the reply.
I have been a developer for many years but I am not familiar with this field at all. Please excuse the massive gaps in my knowledge. We have lost the developer who was doing this and I am looking for someone else. Meanwhile I need to get this working one way or another.
For this application we have an app configured (Native) with the audience and callback all working. It is getting a valid token from Auth0. The Xamarin code is:
namespace AnnoLog.Features.Login
{
public class LoginViewModel : BaseViewModel
{
private readonly IAnnoLogService annoLogService;
private readonly IAuthService authenticationService;
public ICommand Login { get; }
public LoginViewModel(IDependencyService dependencyService) : base(dependencyService)
{
annoLogService = DependencyService.Get<IAnnoLogService>();
authenticationService = DependencyService.Get<IAuthService>();
this.Login = ReactiveCommand.CreateFromTask(async () =>
{
this.IsLoading = true;
var authenticationResult = await authenticationService.Authenticate();
if (!authenticationResult.IsError)
{
var accessToken = authenticationResult.AccessToken;
var claims = authenticationResult.UserClaims.GroupBy(c => c.Type).Select(c => c.First()).ToDictionary(c => c.Type, c => c.Value);
;//token is available here
It retrieves a token that looks valid when pasted into jwt.io. The
"iss": "https://dev-7cdpp4yt.us.auth0.com/",
"sub": "auth0|61e41b9f690cd100686f9240",
"aud": [
"https://rklogintest.azurewebsites.net/",
"https://dev-7cdpp4yt.us.auth0.com/userinfo"
],
"iat": 1644493115,
"exp": 1644579515,
"azp": "JX2j0SonmwEu0yIUH6TQn5WaIOQO1bkZ",
"scope": "openid profile email"
}
This all looks fine, and at this point it gets weird.
We have an API configured, but because the app is Native, it is not in the Machine to Machine Applications tab. There is also no Permissions set in the Permissions tab because I have no idea how to configure them or if they are needed. I can see nothing to connect the configured App with the configured API.
The API was built on .NET 4.7.2 so I put all the API functions into a DLL project and created a .NET Core API to call the functions. This works well when authentication is not enabled.
In accordance with the instructions in the API, the startup.cs, which is pasted below.
I am sure something is missing.
I also posted another message to this forum yesterday. I created an entirely new API but the results are exactly the same.
Thanks,
Rob
Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace AnnoLogAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson(); //this is required because the standard Microsoft JSON will not serialize the output from SpudData
//services.AddMvc()
// .AddNewtonsoftJson();
//1.Add Authentication Services
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://dev-7cdpp4yt.us.auth0.com/";
//options.Audience = "https://localhost:44369/api/";
options.Audience = "https://rklogintest.azurewebsites.net/";
});
services.AddControllers(o => o.InputFormatters.Insert(o.InputFormatters.Count, new TextPlainInputFormatter()));
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//else
//{
// app.UseExceptionHandler("/Home/Error");
//}
app.UseHttpsRedirection();
app.UseRouting();
//app.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStaticFiles();
// 2. Enable authentication middleware
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}