Add auth
This commit is contained in:
parent
6c56d01266
commit
53019ce377
36
ManagementPage/ManagementPage/Auth/AccountBinder.cs
Normal file
36
ManagementPage/ManagementPage/Auth/AccountBinder.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
ManagementPage/ManagementPage/Auth/OidcOptions.cs
Normal file
9
ManagementPage/ManagementPage/Auth/OidcOptions.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -8,12 +8,14 @@ namespace ManagementPage.Database
|
|||||||
public class MongoDbClient
|
public class MongoDbClient
|
||||||
{
|
{
|
||||||
private const string _collectionName = "Main";
|
private const string _collectionName = "Main";
|
||||||
|
private const string _accountsCollectionName = "Accounts";
|
||||||
private const string _databaseName = "Environment";
|
private const string _databaseName = "Environment";
|
||||||
private const string _uri = "mongodb://192.168.0.159:27017";
|
private const string _uri = "mongodb://192.168.0.159:27017";
|
||||||
|
|
||||||
private readonly IMongoDatabase _database;
|
private readonly IMongoDatabase _database;
|
||||||
private readonly MongoClient _client;
|
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()
|
public MongoDbClient()
|
||||||
{
|
{
|
||||||
@ -24,7 +26,8 @@ namespace ManagementPage.Database
|
|||||||
cm.AutoMap();
|
cm.AutoMap();
|
||||||
});
|
});
|
||||||
|
|
||||||
collection = _database.GetCollection<EnvironmentData>(_collectionName);
|
Collection = _database.GetCollection<EnvironmentData>(_collectionName);
|
||||||
|
AccountsCollection = _database.GetCollection<UserData>(_accountsCollectionName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.10" />
|
||||||
<PackageReference Include="MongoDB.Bson" Version="2.13.1" />
|
<PackageReference Include="MongoDB.Bson" Version="2.13.1" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
|
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
29
ManagementPage/ManagementPage/Models/UserData.cs
Normal file
29
ManagementPage/ManagementPage/Models/UserData.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
@page
|
@page
|
||||||
@model ManagementPage.Pages.UserHomeModel
|
@model ManagementPage.Pages.UserHomeModel
|
||||||
@{
|
@{
|
||||||
|
ViewData["Title"] = "UserPage";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<h3>Hi @Model.name</h3>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h2>Current Conditions</h2>
|
<h2>Current Conditions</h2>
|
||||||
<div>
|
<div>
|
||||||
|
@ -1,21 +1,28 @@
|
|||||||
|
using ManagementPage.Auth;
|
||||||
using ManagementPage.Database;
|
using ManagementPage.Database;
|
||||||
using ManagementPage.Models;
|
using ManagementPage.Models;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson.IO;
|
using MongoDB.Bson.IO;
|
||||||
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace ManagementPage.Pages
|
namespace ManagementPage.Pages
|
||||||
{
|
{
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
public class UserHomeModel : PageModel
|
public class UserHomeModel : PageModel
|
||||||
{
|
{
|
||||||
public List<EnvironmentData> data;
|
public List<EnvironmentData> data;
|
||||||
public List<DataSet> humidity;
|
public List<DataSet> humidity;
|
||||||
public List<DataSet> temperature;
|
public List<DataSet> temperature;
|
||||||
|
public string name;
|
||||||
|
|
||||||
private readonly MongoDbClient _dbClient;
|
private readonly MongoDbClient _dbClient;
|
||||||
|
|
||||||
public UserHomeModel(MongoDbClient dbClient)
|
public UserHomeModel(MongoDbClient dbClient)
|
||||||
{
|
{
|
||||||
data = new List<EnvironmentData>();
|
data = new List<EnvironmentData>();
|
||||||
@ -23,14 +30,20 @@ namespace ManagementPage.Pages
|
|||||||
temperature = new List<DataSet>();
|
temperature = new List<DataSet>();
|
||||||
_dbClient = dbClient;
|
_dbClient = dbClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnGetAsync()
|
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>
|
FindOptions<EnvironmentData> options = new FindOptions<EnvironmentData>
|
||||||
{
|
{
|
||||||
Limit = 100,
|
Limit = 100,
|
||||||
NoCursorTimeout = false
|
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());
|
data.AddRange(await cursor.ToListAsync());
|
||||||
foreach (var item in data)
|
foreach (var item in data)
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,34 @@
|
|||||||
|
using ManagementPage.Auth;
|
||||||
using ManagementPage.Database;
|
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 builder = WebApplication.CreateBuilder(args);
|
||||||
|
var Configuration = builder.Configuration;
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddSingleton<MongoDbClient>();
|
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();
|
var app = builder.Build();
|
||||||
|
|
||||||
@ -20,11 +43,18 @@ if (!app.Environment.IsDevelopment())
|
|||||||
//app.UseHttpsRedirection();
|
//app.UseHttpsRedirection();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||||
|
{
|
||||||
|
RequireHeaderSymmetry = false,
|
||||||
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||||
|
});
|
||||||
|
|
||||||
app.MapRazorPages();
|
app.MapRazorPages();
|
||||||
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "https://localhost:7034;http://+:80",
|
"applicationUrl": "https://localhost:8800;http://+:80",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,8 @@
|
|||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"oidc": {
|
||||||
|
"region": "openid-connect",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,7 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"oidc": {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user