gpt4 book ai didi

asp.net-mvc - ASP.NET Web api - 设置自定义 IIdentity 或 IPrincipal

转载 作者:行者123 更新时间:2023-12-01 23:26:03 29 4
gpt4 key购买 nike

在我们的 asp.net mvc/web api 项目中,我们想要使用 AuthorizeAttribute 自定义授权。我们注意到有两个不同的 AuthorizeAttribute,一个位于 MVC 的 System.Web.MVC 命名空间中,另一个位于 System.Net.Http 中Web API 的命名空间。

它在 MVC 中工作,我们的代码如下:

public class MyPrincipal : IPrincipal
{
//some custom properties
public bool IsValid()
{
//custom authentication logic
}

private IIdentity identity;
public IIdentity Identity
{
get { return this.identity; }
}

public bool IsInRole(string role)
{
return true;
}
}

//override AuthorizeCore
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
MyPrincipal user = new MyPrincipal();

if (user.isValid())
{
httpContext.User = user;
}
else
{
httpContext.Response.Redirect("~/Common/NoAuthorize", true);
}
}
}

[MyAuthorizeAttribute]
public class BaseMyController : Controller
{
protected virtual new MyPrincipal User
{
get { return HttpContext.User as MyPrincipal; }
}
}

然后在MVC Controller 中,我们可以通过MyPrincipal用户属性获取用户信息。

但是,当我们开始在 Web api 中使用相同的方式时,我们发现 Web api 没有 HttpContext 属性,并且在 System.Web.Http.AuthorizeAttribute 中,要重写的方法接受 HttpActionContext 参数,它也没有 HttpContext 属性或其他一些我们可以设置 MyPrincipal 实例的地方。

我注意到System.Web.Http.AuthorizeAttribute摘要显示

Specifies the authorization filter that verifies the request's IPrincipal

似乎还有其他方法可以设置 IPrincipal 实例。

我不太清楚,有什么好的建议吗?顺便问一下,为什么asp.net web api Controller 没有HttpContext?有相关的设计模式吗?

相关问题 ASP.NET MVC - Set custom IIdentity or IPrincipal

最佳答案

我实现了一些东西作为概念证明,主要遵循以下内容:Authentication Filters in ASP.NET Web API 2

对于 Web API,您可以创建一个属性 IAuthenticationFilter。如果我没记错的话,你可以将它作为过滤器添加到WebApiConfig中的全局过滤器

config.Filters.Add(new YourAuthenticationAttribute());

或者您可以将其用作 api Controller /方法的属性。

然后,您可以实现 AuthenticateAsync,获取请求的授权 header ,检查方案并验证参数,如果全部有效,则设置主体。

我认为这个想法是,您可以在链中添加多个这些过滤器,并且它们都可以根据一组特定的要求进行身份验证,例如它们寻找的方案,以及在链中的某个位置设置主体,或者挑战已返回。

public class YourAuthenticationAttribute : Attribute, IAuthenticationFilter
{

public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
HttpRequestMessage request = context.Request;

if (request.Headers.Authorization != null &&
request.Headers.Authorization.Scheme.Equals("yourScheme", StringComparison.OrdinalIgnoreCase))
{
// get the value sent with the header.
string authParam = request.Headers.Authorization.Parameter;

// do some validation on the parameter provided...
// if it's all valid, create a principal with claims:


List<Claim> claims = new List<Claim>()
{
new Claim(ClaimTypes.Name, "Eddie Admin"),
new Claim(ClaimTypes.Role, "Admin"),
// new Claim(ClaimTypes.Role, "Delete"),
};

// create an identity with the valid claims.
ClaimsIdentity identity = new ClaimsIdentity(claims, "yourScheme");

// set the context principal.
context.Principal = new ClaimsPrincipal(new[] { identity });

创建主体时,您可以应用声明,并根据正常的授权属性检查这些声明。例如

[Authorize(Roles = "Admin")]

除了这样做之外,我还没有使用过它,但希望这能为您指明正确的方向。

关于asp.net-mvc - ASP.NET Web api - 设置自定义 IIdentity 或 IPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31740224/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com