C# owin oauth发送附加参数

C# owin oauth发送附加参数,c#,security,authentication,oauth,owin,C#,Security,Authentication,Oauth,Owin,我相信这是可能的,但不确定如何实现。我有一个OWIN OAUTH实现,它当前接受用户的用户名和密码,并根据数据库对其进行身份验证。我想将此扩展为传入智能卡Uid,以支持使用智能卡进行单点登录 我可以在OWIN登录中传入其他参数吗?如果可以,如何传入?基本前提是用户可以使用用户名/密码组合或智能卡uid登录(如果传递智能卡uid,并且该uid在数据库中找到,则应用程序将让用户登录) 我目前正在输入用户名、密码和授权类型,我想将uid添加到该列表中,并在我的授权服务提供商中获取该信息 我可以在OAu

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

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

我目前正在输入
用户名
密码
授权类型
,我想将
uid
添加到该列表中,并在我的
授权服务提供商
中获取该信息

我可以在
OAuthGrantResourceOwnerCredentialsContext
上看到
UserName
Password
ClientId
,但我看不到任何其他支持我尝试实现的属性

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

    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);
    }
public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
context.OwinContext.Response.Headers.Add(“访问控制允许来源”,新[]{“*”});
var user=wait this.\u userService.FindUser(context.UserName,context.Password);
if(user==null)
{
SetError(“无效的授权”,“用户名或密码不正确”);
返回;
}
var identity=newclaimsidentity(context.Options.AuthenticationType);
AddClaim(新声明(ClaimTypes.Sid,user.Id.ToString());
identity.AddClaim(新声明(ClaimTypes.Role,“用户”);
identity.AddClaim(新声明(“sub”,context.UserName));
var secretKeyBytes=Encoding.UTF8.GetBytes(user.PasswordHash);
var道具=
新的AuthenticationProperties(
新词典
{
{“dm:appid”,user.Id.ToString()},
{“dm:apikey”,Convert.ToBase64String(secretKeyBytes)}
});
var票证=新的身份验证票证(身份、道具);
上下文。已验证(票证);
}

我也希望能够从上下文中获取Uid,但看不到实现这一点的任何方法,非常感谢您提供的任何帮助。

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

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

Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
接收一个
OAuthValidateClientAuthenticationContext
,其中包含您在发布到授权服务器时传递的其他字段

在上面的图片中,我在第四个位置添加了一个额外的参数
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);
如果您想了解更多信息,可以查看我传递对象的github:

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

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");
$.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
    }
});

非常感谢,在读取参数时做了一点小小的更改,以防它在第一时间未被传递,除此之外,这正是我要找的,谢谢我帮助了Neil。@LeftyX嗨。我正在尝试使用客户端凭据编写自己的oauth服务器。但我被夹在两者之间。我能得到一些帮助吗@user3132179我知道这已经有一段时间了,但您的具体问题是什么,我建议添加一个问题并详细说明您的问题,然后您可能会得到答案和可能的解决方案solution@zeroflaw:可能吧,但我找不到。关于这个问题,有很多有趣的博客值得一读。是我找到的最好的。干杯
$.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
    }
});