【问题标题】:owin oauth send additional parametersowin oauth 发送附加参数
【发布时间】:2015-10-05 04:47:44
【问题描述】:

我确信这是可能的,但不确定如何实现。我有一个 OWIN OAUTH 实现,它当前接受用户的用户名和密码,并根据数据库对它们进行身份验证。我想扩展它以传入 SmartCard Uid 以支持使用 SmartCard 进行单点登录。

我可以在 OWIN 登录中传递其他参数吗?如果可以,如何?基本前提是用户可以使用用户名/密码组合或 SmartCard uid 登录(如果传递 SmartCard uid 并且在数据库中找到,则应用程序将登录用户)

我目前正在传递usernamepasswordgrant_type,我想将uid 添加到该列表中并在我的AuthorizationServiceProvider 中选择它。

我可以在OAuthGrantResourceOwnerCredentialsContext 上看到UserNamePasswordClientId,但我看不到任何其他支持我想要实现的属性。

这是我目前在我的服务提供商中拥有的

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var user = await this._userService.FindUser(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim(ClaimTypes.Sid, user.Id.ToString()));
        identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
        identity.AddClaim(new Claim("sub", context.UserName));

        var secretKeyBytes = Encoding.UTF8.GetBytes(user.PasswordHash);
        var props =
            new AuthenticationProperties(
                new Dictionary<string, string>
                    {
                        { "dm:appid", user.Id.ToString() },
                        { "dm:apikey", Convert.ToBase64String(secretKeyBytes) }
                    });

        var ticket = new AuthenticationTicket(identity, props);
        context.Validated(ticket);
    }

我也希望能够从上下文中获取 Uid,但无论如何都看不到实现这一点,非常感谢任何帮助。

【问题讨论】:

    标签: c# security authentication oauth owin


    【解决方案1】:

    如果你还没有这样做,你必须实现ValidateClientAuthentication

    这是您应该验证您的客户的地方。 在这种方法中,您将对您的客户端进行某种验证并设置可以在GrantResourceOwnerCredentials 中读取的对象/变量。

    Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    

    收到一个OAuthValidateClientAuthenticationContext,其中包含您在向授权服务器发布时传递的附加字段。

    在上图中,我在第 4 位添加了一个额外的参数 uid

    在验证上下文之前:

    context.Validated(clientId);
    

    你可以设置你的变量/对象:

    string uid = context.Parameters.Where(f => f.Key == "uid").Select(f => f.Value).SingleOrDefault()[0];
    context.OwinContext.Set<string>("SmartCard", uid);
    

    现在,在您的 GrantResourceOwnerCredentials 中,您可以简单地读取并使用该值:

    string uid = context.OwinContext.Get<string>("SmartCard");
    

    如果您想了解更多信息,可以查看这个 github repository 我在其中传递了一个对象:

    context.OwinContext.Set<ApplicationClient>("oauth:client", client);
    

    如果您下载整个解决方案,您可以使用 javascript/jquery 客户端对其进行测试。

    更新

    您将在您的 http POST 请求中传递一个附加参数(IE:uid):

    原版 Js

    var request = new XMLHttpRequest();
    request.open('POST', oAuth.AuthorizationServer, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
    request.setRequestHeader('Accept', 'application/json');
    request.send("username=John&password=Smith&grant_type=password&uid=b17ac911-4cf1-4a3e-84a9-beac7b9da157");
    

    jQuery

    $.ajax({
            type: 'POST',
            url: oAuth.AuthorizationServer,
            data: { username: 'John', password: 'Smith', grant_type: 'password', uid: 'b17ac911-4cf1-4a3e-84a9-beac7b9da157' },
            dataType: "json",
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            xhrFields: {
            withCredentials: true
        },
            // crossDomain: true,
            headers: {
                    'Authorization': 'Basic ' + authorizationBasic
        }
    });
    

    【讨论】:

    • 非常感谢,在读取参数时稍作更改,以防一开始就没有传递参数,除此之外,这正是我想要的,谢谢
    • 很高兴我帮助了尼尔。
    • @LeftyX 嗨。我正在尝试使用 client_credentials 编写我自己的 oauth 服务器。但我被困在两者之间。我能得到一些帮助吗? github.com/koushiksaha89/oauthserver
    • @user3132179 我知道这已经有一段时间了,但是您的具体问题是什么,我建议您添加一个问题并详细说明您的问题,然后您可能会得到答案和可能的解决方案跨度>
    • @zeroflaw:可能,但我找不到。关于这个主题有很多有趣的博客值得一读。 BitOfTech 是我找到的最好的。干杯。
    猜你喜欢
    • 2018-08-29
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2011-03-11
    • 1970-01-01
    • 2016-10-17
    • 2017-09-19
    相关资源
    最近更新 更多