Skip to content

Commit

Permalink
feat(src/plugincore.aspnetcore): accountManager 部分方法静态化, 提供 HttpConte…
Browse files Browse the repository at this point in the history
…xt 传入方式, 相关引用处更新调用
  • Loading branch information
yiyungent committed Feb 15, 2024
1 parent 6a03628 commit 491f334
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
return AuthenticateResult.NoResult();
}

bool isAdmin = this._accountManager.IsAdminToken(token);
bool isAdmin = AccountManager.IsAdminToken(token);

if (!isAdmin)
{
Expand Down
21 changes: 14 additions & 7 deletions src/PluginCore.AspNetCore/Authorization/AccountManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public static Config.PluginCoreConfig.AdminModel Admin
}
}

public string CurrentToken()
public static string CurrentToken(HttpContext httpContext)
{
string token = null;
HttpRequest request = HttpContext.Request;
HttpRequest request = httpContext.Request;
try
{
// header -> cookie
Expand Down Expand Up @@ -94,10 +94,14 @@ public string CurrentToken()
throw ex;
}


return token;
}

public string CurrentToken()
{
return CurrentToken(this.HttpContext);
}

public static string CreateToken()
{
return CreateToken(Admin.UserName, Admin.Password);
Expand All @@ -111,21 +115,25 @@ public static string CreateToken(string userName, string password)
return token;
}

public bool IsAdminToken(string token)
public static bool IsAdminToken(string token)
{
bool isAdmin = false;
isAdmin = CreateToken().Equals(token);

return isAdmin;
}


public bool IsAdmin()
{
return IsAdmin(this.HttpContext);
}

public static bool IsAdmin(HttpContext httpContext)
{
bool isAdmin = false;
try
{
string currentToken = CurrentToken();
string currentToken = CurrentToken(httpContext);
isAdmin = IsAdminToken(currentToken);
}
catch (Exception ex)
Expand All @@ -135,7 +143,6 @@ public bool IsAdmin()

return isAdmin;
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PluginCore.AspNetCore/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<ActionResult<BaseResponseModel>> Login([FromBody] LoginRequest
try
{
string token = AccountManager.CreateToken(requestModel.UserName, requestModel.Password);
bool isAdmin = this._accountManager.IsAdminToken(token);
bool isAdmin = AccountManager.IsAdminToken(token);
if (!isAdmin)
{
responseModel.Code = -1;
Expand Down

0 comments on commit 491f334

Please sign in to comment.