我正在开发一个Intranet asp.net核心Web API应用程序。身份验证的要求是:
现在,我到目前为止:
我正在使用Claims Middlewere来检查用户的Active Directory组,让我们说这样的话:
public class ClaimsTransformer : IClaimsTransformation {
private readonly IAuthorizationService _authorizationService;
public ClaimsTransformer(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
_authorizationService.Authorize(principal as IHmiClaimsPrincipal);
return Task.FromResult(principal);
}}
Run Code Online (Sandbox Code Playgroud)我还在服务配置中指定了特殊策略,例如:
services.AddAuthorization(options =>
{
options.AddPolicy(“ TestPolicy”,策略=>
policy.RequireClaim(ClaimTypes.Role,“ TestUser”));
options.AddPolicy(“ TestPolicy2”,策略=>
policy.RequireClaim(ClaimTypes.Role,“ SuperUser”));
});我将 [Authorize]属性与特定策略结合使用,以便基于策略限制对特定资源的访问
现在的问题是,我应该如何满足REQ3?
简化,我有这两种Extension方法:
public static class Extensions
{
public static string GetString(this Exception e)
{
return "Standard!!!";
}
public static string GetString(this TimeoutException e)
{
return "TimeOut!!!";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用它们的地方:
try
{
throw new TimeoutException();
}
catch (Exception e)
{
Type t = e.GetType(); //At debugging this a TimeoutException
Console.WriteLine(e.GetString()); //Prints: Standard
}
Run Code Online (Sandbox Code Playgroud)
我有更多的GetString()扩展.
我try{...}catch{...}变得越来越大,基本上我搜索方法将其缩短为1个捕获,根据异常的类型调用扩展.
有没有办法在运行时调用正确的扩展方法?
概观
当我上课时
public class RequestEntry
{
public int RequestId
{
get;
set;
}
public string ApproverUserId
{
get;
set;
}
public string ApproverUserName
{
get;
set;
}
public DateTime RequestedDate
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
它包含在列表中:
List<RequestEntry> RequestEntries = new List<RequestEntry>()
{
new RequestEntry{RequestId = 1, RequestedDate = new DateTime(2018,06,01), ApproverUserId = "STEVES", ApproverUserName = "Steve Smith"},
new RequestEntry{RequestId = 1, RequestedDate = new DateTime(2018,06,01), ApproverUserId = "GRAHAMS", ApproverUserName = "Graham Smith" },
new RequestEntry{RequestId = 2, RequestedDate = new …Run Code Online (Sandbox Code Playgroud) 我使用 SmtpClient 发送电子邮件如下代码:
SmtpClient client = new SmtpClient("host", 587);
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("user", "pass");
MailAddress from = new MailAddress("from_mail", String.Empty, System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("to_mail");
MailMessage message = new MailMessage(from, to);
message.Body = "Say hello";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Testing";
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.Send(message);
Run Code Online (Sandbox Code Playgroud)
如果我使用 .Net Framework 4.5,它将引发错误:由于意外的数据包格式,握手失败。如果我使用 .Net Framework 4.6.1,它将成功发送邮件。
请给我推荐?