小编Mik*_*ike的帖子

Windows身份验证-特殊用户需要其他密码

我正在开发一个Intranet asp.net核心Web API应用程序。身份验证的要求是:

  • REQ1-当试图访问该网站的用户不在Active Directory的特殊组中(让其命名为“ commonUsers”)时,它根本没有被授权
  • REQ2-当尝试访问该网站的用户位于Active Directory的组“ commonUsers”中时,将被授权并返回Web资源
  • REQ3-当试图访问该网站的用户位于Active Directory的“ superUser”组中时,需要再次提示其输入域密码(因为它试图访问一些非常受限的资源)

现在,我到目前为止:

  • 我的服务使用http.sys服务器托管,以支持Windows身份验证。
  • 我正在使用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?

c# windows-authentication asp.net-core

12
推荐指数
1
解决办法
286
查看次数

使用派生类型调用扩展方法的重载

简化,我有这两种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个捕获,根据异常的类型调用扩展.

有没有办法在运行时调用正确的扩展方法?

c# extension-methods casting

11
推荐指数
1
解决办法
215
查看次数

Linq Group结果为Object

概观

当我上课时

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)

c# linq

4
推荐指数
1
解决办法
71
查看次数

“由于意外的数据包格式,握手失败”与 SmtpClient

我使用 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,它将成功发送邮件。

请给我推荐?

.net c#

1
推荐指数
1
解决办法
7137
查看次数