This commit is contained in:
Simon 2021-09-29 01:41:57 +01:00
parent 6c56d01266
commit 53019ce377
11 changed files with 135 additions and 6 deletions

View File

@ -0,0 +1,36 @@
using ManagementPage.Database;
using ManagementPage.Models;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ManagementPage.Auth
{
public class AccountBinder
{
private readonly MongoDbClient _dbClient;
public AccountBinder(MongoDbClient dbClient)
{
_dbClient = dbClient;
}
public async Task<UserData> GetLocalAccount(string OpenID)
{
// lookup ID
var filter = new BsonDocument("OpenId", OpenID );
var userData = await _dbClient.AccountsCollection.Find(filter).SingleOrDefaultAsync();
if(userData == null)
{
userData = await BindNewAccount(OpenID);
}
return userData;
}
private async Task<UserData> BindNewAccount(string OpenID)
{
var user = new UserData(OpenID, ObjectId.GenerateNewId(), new List<ObjectId>());
await _dbClient.AccountsCollection.InsertOneAsync(user);
return user;
}
}
}

View File

@ -0,0 +1,9 @@
namespace ManagementPage.Auth
{
public class OidcOptions
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string Region { get; set; }
}
}

View File

@ -8,12 +8,14 @@ namespace ManagementPage.Database
public class MongoDbClient
{
private const string _collectionName = "Main";
private const string _accountsCollectionName = "Accounts";
private const string _databaseName = "Environment";
private const string _uri = "mongodb://192.168.0.159:27017";
private readonly IMongoDatabase _database;
private readonly MongoClient _client;
public IMongoCollection<EnvironmentData> collection { get; private set; }
public IMongoCollection<EnvironmentData> Collection { get; private set; }
public IMongoCollection<UserData> AccountsCollection { get; private set; }
public MongoDbClient()
{
@ -24,7 +26,8 @@ namespace ManagementPage.Database
cm.AutoMap();
});
collection = _database.GetCollection<EnvironmentData>(_collectionName);
Collection = _database.GetCollection<EnvironmentData>(_collectionName);
AccountsCollection = _database.GetCollection<UserData>(_accountsCollectionName);
}
}
}

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.10" />
<PackageReference Include="MongoDB.Bson" Version="2.13.1" />
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
</ItemGroup>

View File

@ -0,0 +1,29 @@
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace ManagementPage.Models
{
public class UserData
{
[BsonId]
public ObjectId _id { get; set; }
[JsonPropertyName("openid")]
public string OpenId { get; set; }
[JsonPropertyName("localid")]
public ObjectId LocalId { get; set; }
[JsonPropertyName("devices")]
public List<ObjectId> Devices { get; set; }
[BsonConstructor]
public UserData(string openId, ObjectId localId, List<ObjectId> devices)
{
OpenId = openId;
LocalId = localId;
Devices = devices;
}
}
}

View File

@ -1,8 +1,11 @@
@page
@model ManagementPage.Pages.UserHomeModel
@{
ViewData["Title"] = "UserPage";
}
<h3>Hi @Model.name</h3>
<div>
<h2>Current Conditions</h2>
<div>

View File

@ -1,21 +1,28 @@
using ManagementPage.Auth;
using ManagementPage.Database;
using ManagementPage.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System.Security.Claims;
namespace ManagementPage.Pages
{
[Authorize]
public class UserHomeModel : PageModel
{
public List<EnvironmentData> data;
public List<DataSet> humidity;
public List<DataSet> temperature;
public string name;
private readonly MongoDbClient _dbClient;
public UserHomeModel(MongoDbClient dbClient)
{
data = new List<EnvironmentData>();
@ -23,14 +30,20 @@ namespace ManagementPage.Pages
temperature = new List<DataSet>();
_dbClient = dbClient;
}
public async Task OnGetAsync()
{
var binder = new AccountBinder(_dbClient);
var id = await binder.GetLocalAccount(User.FindFirstValue(ClaimTypes.NameIdentifier));
name = User.FindFirstValue(ClaimTypes.GivenName);
FindOptions<EnvironmentData> options = new FindOptions<EnvironmentData>
{
Limit = 100,
NoCursorTimeout = false
};
using var cursor = await _dbClient.collection.FindAsync(new BsonDocument(), options);
using var cursor = await _dbClient.Collection.FindAsync(new BsonDocument(), options);
data.AddRange(await cursor.ToListAsync());
foreach (var item in data)
{

View File

@ -1,11 +1,34 @@
using ManagementPage.Auth;
using ManagementPage.Database;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
var Configuration = builder.Configuration;
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers();
builder.Services.AddSingleton<MongoDbClient>();
// Allow sign in via an OpenId Connect provider like OneLogin
builder.Services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ClientId = Configuration["oidc:clientid"];
options.ClientSecret = Configuration["oidc:clientsecret"];
options.Authority = @"https://auth.jacknet.io/auth/realms/JackNet";
//options.Authority = String.Format("https://{0}.onelogin.com/oidc/2", Configuration["oidc:region"]);
options.ResponseType = "code";
options.GetClaimsFromUserInfoEndpoint = true;
}
);
var app = builder.Build();
@ -20,11 +43,18 @@ if (!app.Environment.IsDevelopment())
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
RequireHeaderSymmetry = false,
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.MapRazorPages();
app.Run();

View File

@ -12,7 +12,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7034;http://+:80",
"applicationUrl": "https://localhost:8800;http://+:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -5,5 +5,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"oidc": {
"region": "openid-connect",
}
}

View File

@ -5,5 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"oidc": {
}
}