如何在 Web API 的 Owin 身份验证的 Provider 类中获取 Windows 登录?
Posted
技术标签:
【中文标题】如何在 Web API 的 Owin 身份验证的 Provider 类中获取 Windows 登录?【英文标题】:How to get Windows logon in Provider class of Owin authentication in Web API? 【发布时间】:2019-10-26 15:17:27 【问题描述】:我想在 Provider 类的 GrantResourceOwnerCredentials 方法中获取用户的 Windows 登录并验证他们。我尝试了以下所有可能的方法,但没有运气。
-
System.Security.Principal.WindowsIdentity.GetCurrent().Name --> 它正在返回服务器名称
Request.LogonUserIdentity --> null(认证前无法访问)
HttpContext.Current.User --> null
【问题讨论】:
也许这会有所帮助:***.com/a/32083020/11266990 我看过这篇文章。它在 Invoke 方法中获取值,但我不确定在哪里调用此方法。如果你能告诉我,那就太好了。我希望在 GrantResourceOwnerCredentials 方法中专门登录,以便在生成令牌之前验证用户。 恐怕我没有在 Web API 中使用 Windows 登录,但如果您使用的是 GrantResourceOwnerCredentials,我希望上下文作为参数传递?这会返回什么:public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) var username = context.UserName; var password = context.Password;
(对不起,我似乎无法在评论中正确格式化代码)
用户名和密码是客户端发送的值。
【参考方案1】:
据我了解,如果您只使用 Windows 身份验证,则无需担心 GrantResourceOwnerCredentials。您是否尝试使用令牌身份验证以及 Windows 身份验证?您应该只对将在您的 Intranet 上运行的 Web Api 使用 Windows 身份验证。
如果我说出你已经知道的话,请原谅我,但根据我所做的研究,我要感谢 Dominick Baier 在复数视力方面,你需要:
安装 Microsoft.Owin 和 Microsoft.Owin.Security.OAuth nuget 包 在项目(F4 属性窗口)或配置文件中将 Windows 身份验证设置为“启用” 确保您的控制器具有 [Authorize] 属性并从 ApiController 继承 具体实现 Owin 中间件(您需要创建三个类并确保它们在 startup.cs 类中进行了配置)看看下面的代码:第一个中间件类:声明函数
public class ClaimsTransformationOptions
public Func<ClaimsPrincipal, Task<ClaimsPrincipal>> ClaimsTransformation get; set;
第二个中间件类:这是 Invoke 方法所在的地方
public class ClaimsTransformationMiddleware
readonly ClaimsTransformationOptions _options;
readonly Func<IDictionary<string, object>, Task> _next;
public ClaimsTransformationMiddleware(Func<IDictionary<string, object>, Task> next, ClaimsTransformationOptions options)
_next = next;
_options = options;
public async Task Invoke(IDictionary<string, object> env)
// use Katana OWIN abstractions (optional)
var context = new OwinContext(env);
if (context.Authentication != null &&
context.Authentication.User != null)
var transformedPrincipal = await _options.ClaimsTransformation(context.Authentication.User);
context.Authentication.User = new ClaimsPrincipal(transformedPrincipal);
await _next(env);
第三个中间件类:这是一个扩展类
public static class ClaimsTransformationMiddlewareExtensions
public static IAppBuilder UseClaimsTransformation(this IAppBuilder app,
Func<ClaimsPrincipal, Task<ClaimsPrincipal>> transformation)
return app.UseClaimsTransformation(new ClaimsTransformationOptions
ClaimsTransformation = transformation
);
public static IAppBuilder UseClaimsTransformation(this IAppBuilder app, ClaimsTransformationOptions options)
if (options == null)
throw new ArgumentNullException("options");
app.Use(typeof(ClaimsTransformationMiddleware), options);
return app;
在启动类中:
public void Configuration(IAppBuilder app)
app.UseClaimsTransformation(Transformation);
private async Task<ClaimsPrincipal> Transformation(ClaimsPrincipal incoming)
if (!incoming.Identity.IsAuthenticated)
return incoming;
var name = incoming.Identity.Name;
// go to a datastore - find the app specific claims
var claims = new List<Claim>
new Claim(ClaimTypes.NameIdentifier, name),
new Claim(ClaimTypes.Role, "foo"),
new Claim(ClaimTypes.Email, "foo@foo.com")
;
var id = new ClaimsIdentity("Windows");
id.AddClaims(claims);
return new ClaimsPrincipal(id);
在Controller中(确保它有[Authorize]属性并且继承自ApiController
public IEnumerable<ViewClaim> Get()
var principal = User as ClaimsPrincipal;
return from c in principal.Claims
select new ViewClaim
Type = c.Type,
Value = c.Value
;
【讨论】:
以上是关于如何在 Web API 的 Owin 身份验证的 Provider 类中获取 Windows 登录?的主要内容,如果未能解决你的问题,请参考以下文章
Web API 2,OWIN 身份验证,SignOut 不注销
实施 Identity 2.1 + OWIN OAuth JWT 不记名令牌时如何从 Web API 控制器端点进行身份验证
身份验证/授权 MVC 5 和 Web API - Katana/Owin