我想对控制器上的所有操作都要求一个策略,并且对HTTP“编辑方法”(POST,PUT,PATCH和DELETE)的所有调用都需要第二个策略。也就是说,编辑方法应同时要求两个策略。由于实现要求,以及保持代码DRY的愿望,我需要将后者策略应用于控制器级别,而不是在所有操作方法上都重复使用。
举一个简单的例子,我有一个PeopleController,我也有两个许可权,分别实现为Policy ViewPeople和EditPeople。现在我有:
[Authorize("ViewPeople")]
public class PeopleController : Controller { }
Run Code Online (Sandbox Code Playgroud)
如何添加EditPeople策略/权限,使其“堆叠”且仅适用于编辑动词?
我遇到了两个似乎都是很痛苦的问题:
我尝试使用自定义Requirement和AuthorizationHandler解决前者,如下所示:
public class ViewEditRolesRequirement : IAuthorizationRequirement
{
public ViewEditRolesRequirement(Roles[] editRoles, Roles[] viewRoles)
=> (EditRoles, ViewRoles) = (editRoles, viewRoles);
public Roles[] EditRoles { get; }
public Roles[] ViewRoles { get; }
}
public class ViewEditRolesHandler : AuthorizationHandler<ViewEditRolesRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ViewEditRolesRequirement requirement)
{
if (context.User != null)
{
var canView = requirement.ViewRoles.Any(r => context.User.IsInRole(r.ToString()));
var …Run Code Online (Sandbox Code Playgroud) 我读过(此处),在 Web API 设置中执行 Try/Catch 块并避免异常并不是一个好主意。但是,如果您想捕获并记录应用程序中发生的错误...... try/catch 不是最好的方法吗?这是我在应用程序中所做的示例 - 我很好奇是否有人有更好的方法。我的ErrorHander班级将错误保存到数据库,并向管理员发送电子邮件详细信息。
namespace MyApp.Controllers
{
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthRepository _repo;
private readonly IErrorHandler _errorHandler;
private AuthController(IAuthRepository repo, IErrorHandler errorHandler) {
_errorHandler = errorHandler;
}
[Authorize]
[HttpPut("changePassword")]
public async Task<IActionResult> ChangePassword(
[FromBody] UserForChangePasswordDto userForChangePasswordDto)
{
var userFromRepo = await _repo.Login(userForChangePasswordDto.Username,
userForChangePasswordDto.OldPassword, null);
try
{
//logic to update user's password and return updated user
return Ok(new { tokenString, user });
}
catch (Exception …Run Code Online (Sandbox Code Playgroud) 我试图通过单击按钮将 html body 的背景颜色更改为红色,但我只知道如何使用已经在 body 内部的元素,而不是 body 本身。
class App extends Component {
constructor(props) {
super(props);
this.state = {
bgColor: ""
};
}
boxClick = e => {
this.setState({
bgColor: "red"
});
};
render() {
return (
<div className="App">
<article className="experimentsHolder">
<div
className="boxClickCss"
style={{ backgroundColor: this.state.bgColor }}
onClick={this.boxClick}
>
Click Me!
</div>
</article>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我添加style={{backgroundColor: this.state.bgColor}}到 div,但我无法将其添加到 body 中,因为它不在此文件中。有什么帮助吗?
刚刚开始研究 RxJS 并试图理解它filter。https://rxjs-dev.firebaseapp.com/api/operators/filter
从表面上看,没有什么需要弄清楚的,我知道吗?:S
这就是我所拥有的——
declare interface App {
id: string;
bId: string;
account: string;
title: string;
platform: string;
isLive: boolean;
}
Run Code Online (Sandbox Code Playgroud)
public allApps: Observable<App[]>;
this.allApps = this.httpClient.get(`${UtilitiesService.APIRoot}/globals/apps`).pipe(
map( response => response as App[] )
);
Run Code Online (Sandbox Code Playgroud)
这get/pipe/map让我得到了对象并将其正确地转换为App[]数组。到目前为止没有任何问题!
现在我想filter退出bid !== null并且只有有效值this.allApps。所以我“尝试”将输出传递map给filter这样的 -
this.allApps = this.httpClient.get(`${UtilitiesService.APIRoot}/globals/apps`).pipe(
map( response => response as App[] ),
filter( app => app.bid === null)
);
Run Code Online (Sandbox Code Playgroud)
就我的阅读/YouTube 视频而言,map( )的输出 …
假设我正在创建车辆本体。车辆及其制造商通过诸如此类的连接MyCar hasManufacturer Tesla。然后,制造商有一个原产国,我将其指定为Tesla hasCountryOfOrigin USA。我要的是运行我的推理机时MyCar要连接的USA,MyCar hasCountryOfOrigin USA。我知道这与传递财产不同。我该如何实现?(专门使用Protege)
我正在从 Java Jax-Rs API 迁移到 ASP.NET。我有传递到 API 的自定义标头参数。
我找不到在 ASP.NET 中执行相同操作的方法。
这是我到目前为止所拥有的:
[HttpPost]
public String login()
{
return "works";
}
Run Code Online (Sandbox Code Playgroud)
我搜索了我找到的每个教程,但找不到任何提及这一点的内容。
frm =(Form)Assembly.GetEntryAssembly().CreateInstance("EditFrom");
return frm;
Run Code Online (Sandbox Code Playgroud)
这将返回null值.如何获得价值
我有一个类定义如下:
class A
{
int contractID;
string name;
string blah;
etc...
}
Run Code Online (Sandbox Code Playgroud)
多个ClassA存储在List()中.我和基于列表中的所有ContractID创建一个List.在LINQ中是否有办法能够执行此操作而不是迭代List()?
为什么这总是回归真的?
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 24;
ICollection<ValidationResult> results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), results);
Console.WriteLine("Valid = {0}",isValid);
foreach (var result in results)
{
Console.WriteLine(result.ErrorMessage);
}
Console.ReadKey();
}
}
public class Person
{
[Required(ErrorMessage = "You have to identify yourself!!")]
public int Id { get; set; }
public decimal Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的用法有什么问题?
我正在尝试在XNA中创建一个保存文件目录,我有一个XML序列化器设置来存储游戏的分数,但是当游戏第一次运行时,我希望它在用户文档中创建一个名为SaveData的文件夹中的文件.我可以创建它的XML文件,但我不知道如何在用户文档中创建它,因为每次用户名都不同?
所以我的问题是如何保存相对于任何用户名的文档?
c# ×7
asp.net-core ×2
angular ×1
exception ×1
file ×1
http-headers ×1
javascript ×1
jax-rs ×1
linq ×1
ontology ×1
owl ×1
protege ×1
rdf ×1
reactjs ×1
rxjs ×1
save ×1
semantic-web ×1
typescript ×1