-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:add authorization for imaotai web and wpf
- Loading branch information
1 parent
b69b6f5
commit 316e5da
Showing
23 changed files
with
233 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,12 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:56107", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:5004", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
"profiles": { | ||
"IMaoTai.MasaBlazor": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
"applicationUrl": "http://localhost:5004;https://localhost:5005" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
using IMaoTai.Core.Domain; | ||
using IMaoTai.Core.Service; | ||
using JsonFlatFileDataStore; | ||
using IMaoTai.Core; | ||
|
||
namespace IMaoTai.MasaUI.Core | ||
{ | ||
public class WHostAuthenticationStateProvider : AuthenticationStateProvider | ||
{ | ||
private readonly ILoginUserService _loginUserService; | ||
|
||
private ClaimsIdentity identity = new ClaimsIdentity(); | ||
|
||
public WHostAuthenticationStateProvider( | ||
ILoginUserService loginUserService) | ||
{ | ||
_loginUserService = loginUserService; | ||
} | ||
|
||
public override async Task<AuthenticationState> GetAuthenticationStateAsync() | ||
{ | ||
// Open database (create new if file doesn't exist) | ||
var store = new DataStore(CommonX.LoginCacheUserListFile); | ||
// Get employee collection | ||
var collection = store.GetCollection<UserLogin>(); | ||
|
||
// Find item with name | ||
var userSession = collection | ||
.AsQueryable() | ||
.FirstOrDefault(); | ||
|
||
if (userSession != null) | ||
{ | ||
var claims = new[] { | ||
new Claim(ClaimTypes.Name, userSession.UserName)}; | ||
identity = new ClaimsIdentity(claims, "IMaoTai"); | ||
} | ||
var user = new ClaimsPrincipal(identity); | ||
return await Task.FromResult( new AuthenticationState(user)); | ||
|
||
} | ||
|
||
|
||
public ClaimsPrincipal GetCurrentUser() | ||
{ | ||
var user = new ClaimsPrincipal(identity); | ||
return user; | ||
} | ||
|
||
public async Task<string> Logout() | ||
{ | ||
var result = await _loginUserService.Logout(); | ||
identity = new ClaimsIdentity(); | ||
if (!Config.IsServer) | ||
{ | ||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); | ||
} | ||
return result; | ||
} | ||
|
||
public async Task<bool> Login(UserLogin loginParameters, string returnUrl) | ||
{ | ||
var result = await _loginUserService.Login(loginParameters, returnUrl); | ||
if (result) | ||
{ | ||
var claims = new[] { new Claim(ClaimTypes.Name, loginParameters.UserName) }; | ||
identity = new ClaimsIdentity(claims, "IMaoTai"); | ||
|
||
// Open database (create new if file doesn't exist) | ||
var store = new DataStore(CommonX.LoginCacheUserListFile); | ||
// Get employee collection | ||
var collection = store.GetCollection<UserLogin>(); | ||
|
||
// Find item with name | ||
var userDynamic = collection | ||
.AsQueryable() | ||
.FirstOrDefault(p => p.UserName == loginParameters.UserName); | ||
if (userDynamic == null) | ||
{ | ||
await collection.InsertOneAsync(loginParameters); | ||
} | ||
else | ||
{ | ||
await collection.UpdateOneAsync(p => p.UserName == loginParameters.UserName, loginParameters); | ||
} | ||
|
||
|
||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
|
||
@code { | ||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.