The code above uses OAuth2 with code flow, however you can enable PKCE flow by uncommenting line 13.
The code snippet above configures authentication as follows:
Sets the default challenge to Roblox OAuth2 and all other schemes to cookie defaults.
Adds cookie authentication
Adds Roblox OAuth2 authentication
You can customize your application by editing the AddRoblox() options.
Authentication is now setup, however users will see a 404 page if they visit the LogInPath because it is not being routed.
Add the following code after builder.Build():
Program.cs
app.UseRobloxOpenCloudRouting();
Your Program.cs file should look like this:
using Microsoft.AspNetCore.Authentication.Cookies;
using OpenCloud.AspNetCore.Defaults;
using OpenCloud.AspNetCore.Extensions;
using OpenCloud.Extensions;
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = builder.Configuration;
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRobloxOpenCloud(options =>
{
options.ClientId = "<YourClientId>";
options.ClientSecret = "<YourClientSecret>";
});
builder.Services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = OpenCloudRobloxAuthenticationDefaults.AuthenticationScheme;
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddRoblox(options =>
{
options.LogInPath = new PathString("/auth/roblox");
options.CallbackPath = new PathString("/auth/roblox/redirect");
options.ReturnPath = new PathString("/");
//options.UsePkce = true;
options.ClaimActions.MapJsonKey("urn:roblox:id", "sub");
options.AllowLogInWhenAuthenticated = true;
options.Scope = OpenCloud.Enums.RobloxOAuthScope.OpenId |
OpenCloud.Enums.RobloxOAuthScope.Profile;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseRobloxOpenCloudRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
If you navigate to /auth/roblox in a browser, you should be redirected to the Roblox authorization page where you will be asked to grant access to your user ID and profile information. Once you authorize your application you should be redirected back to ReturnPath. If authentication is successful, OpenCloud.NET will save the user's ID as a claim.