繁体   English   中英

如何在Web API中获取Owin身份验证的Provider类中的Windows登录?

[英]How to get Windows logon in Provider class of Owin authentication in Web API?

我想在Provider类的GrantResourceOwnerCredentials方法中获取用户的Windows登录并验证它们。 我尝试了下面所有可能的方法,但没有运气。

  1. System.Security.Principal.WindowsIdentity.GetCurrent()。Name - >它返回服务器名称
  2. Request.LogonUserIdentity - > null(在身份验证之前无法访问)
  3. HttpContext.Current.User - > null

据我所知,如果您只使用Windows身份验证,则无需担心GrantResourceOwnerCredentials。 您是否尝试使用令牌身份验证以及Windows身份验证? 您应该只对要在Intranet上运行的Web Api使用Windows身份验证。

如果我说出你已经知道的事情,请原谅我,但是从我做过的研究中,感谢Dominick Baier的多元化,你需要:

  • 安装Microsoft.Owin和Microsoft.Owin.Security.OAuth nuget包
  • 在项目(F4属性窗口)或配置文件中将Windows身份验证设置为“已启用”
  • 确保控制器上有[Authorize]属性并从ApiController继承
  • 具体实现Owin中间件(您需要创建三个类并确保它们在startup.cs类中配置)看看下面的代码:

1st Middleware类:声明函数

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);
    }
}

第3中间件类:这是一个扩展类

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
            };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM