ASP.NET Core-使用Windows身份验证进行授权 - c#

我已将我的Web API配置为与Windows身份验证一起使用。我的目标实质上是根据用户的Windows帐户来限制控制器中的某些操作。一些将能够执行读取操作,而其他一些将能够执行将写入基础数据库的操作。我找到了大量有关如何设置基于声明的授权的文档,这是我认为我需要走的路。我还没有找到如何使用Windows身份验证进行设置。我想我缺少中间步骤,例如将Windows Auth注册为身份提供者?

startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddAuthentication(IISDefaults.AuthenticationScheme);

    services.AddAuthorization(options =>
    {
        options.AddPolicy("readOnly", policy =>
                          policy.RequireClaim(`???????????????????????`));
        options.AddPolicy("write", policy =>
                          policy.RequireClaim(`???????????????????????`));
    });
}

控制者

[Authorize(Policy = "ReadOnly")]
public class MyController : Controller
{
    public ActionResult SomeReadOnlyAction()
    {
        //Return data from database
    }

    [Authorize(Policy = "Write")]
    public ActionResult AWriteAction()
    {
        //Create/Update/Delete data from database
    }
}

我猜想问这个问题的另一种方法是如何使用Windows身份验证配置或访问声明/角色等。

参考方案

看来您想通过策略使用基于声明的授权。在您的应用程序中设置Windows身份验证后,您可以向ClaimsPrincipal添加自定义声明,检查用户的身份并确认当前用户具有以下权限:

您可以向您的应用程序添加声明转换服务:

class ClaimsTransformer : IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        var id = ((ClaimsIdentity)principal.Identity);

        var ci = new ClaimsIdentity(id.Claims, id.AuthenticationType, id.NameClaimType, id.RoleClaimType);
        if (ci.Name.Equals("name"))
        {
            ci.AddClaim(new Claim("permission", "readOnly"));
        }
        else
        {
            ci.AddClaim(new Claim("permission", "write"));

        }


        var cp = new ClaimsPrincipal(ci);

        return Task.FromResult(cp);
    }
}

添加到Startup.cs(.net Core 2.0)中:

    services.AddTransient<IClaimsTransformation, ClaimsTransformer>();

制定政策:

    services.AddAuthorization(options =>
    {
        options.AddPolicy("Readonly", policy =>
                          policy.RequireClaim("permission", "readOnly"));

        options.AddPolicy("Write", policy =>
                        policy.RequireClaim("permission", "write"));
    });

通过要求以下策略来限制对控制器或操作的访问:

    [Authorize(Policy = "Write")]
    public IActionResult Contact()
    {
        ViewData["Message"] = "Your contact page.";

        return View();
    }

如果您已经在AD中添加了组(写,只读)并将相关用户添加到group中,则还可以检查组:

public static class Security
{
    public static bool IsInGroup(this ClaimsPrincipal User, string GroupName)
    {
        var groups = new List<string>();

        var wi = (WindowsIdentity)User.Identity;
        if (wi.Groups != null)
        {
            foreach (var group in wi.Groups)
            {
                try
                {
                    groups.Add(group.Translate(typeof(NTAccount)).ToString());
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            return groups.Contains(GroupName);
        }
        return false;
    }
}

并使用像:

 if (User.IsInGroup("GroupName"))
 {

 }

ASP.NET Core-在Singleton注入上存储库依赖项注入失败 - c#

我正在使用SoapCore为我的ASP.NET Core MVC应用程序创建Web服务。我正在使用Entity Framework Core和简单的存储库模式来获取我的数据库数据。我通过Startup.cs中的.AddSingleton()注入存储库类:services.AddSingleton<IImportRepository, ImportRep…

如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#

我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

在ASP.NET Core 2.1中添加自定义TagHelpers - c#

我严格按照ASP.NET Core文档进行操作,并花费了大量时间来拖曳堆栈溢出,试图实现简单的自定义TagHelper,但没有成功。任何人都可以就任何陷阱或已知错误提出建议吗?应用程序属性:AssemblyName: AmpWeb Target Framework .NET Core 2.1 NuGet软件包Microsoft.AspNetCore.All …

ASP.NET Core 2.0中的多个身份 - c#

我正在将ASP.NET Core 1.0应用程序迁移到ASP.NET Core 2.0。在我的启动中,我正在配置两个身份:services.AddIdentity<IdentityUser, IdentityRole>(configureIdentity) .AddDefaultTokenProviders() .AddUserStore<…