OAuth2 WebAPI附加标签

问题描述 投票:0回答:1

是否有任何方法可以在OAuth的json结果上添加其他标签?现在,我得到了这个结果。

{
    "access_token": "...",
    "token_type": "bearer",
    "expires_in": 3599
}

我需要添加的是

 ".expires": "...",
  ".issued": "..."

这是我的代码段:

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    return Task.Factory.StartNew(() =>
    {
        var username = context.UserName;
        var password = context.Password;
        var userService = new UserService();
        User user = userService.GetUserByCredentials(username, password);
        if (user != null)
        {
            var claims = new List<Claim>()
            {
                new Claim(ClaimTypes.Name, user.Name),
                new Claim("UserID", user.Id)
            };

            ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);
            context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));
        }
        else
        {
            context.SetError("invalid_grant", "Error");
        }
    });
}

也没有显示其他属性,它仅应将姓,年龄,性别返回为我的样本数据。

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        return Task.Factory.StartNew(() =>
        {
            var username = context.UserName;
            var password = context.Password;
            var userService = new UserService();
            User user = userService.GetUserByCredentials(username, password);
            if (user != null)
            {
                var claims = new List<Claim>()
                {
                    new Claim(ClaimTypes.Name, user.Name),
                    new Claim("UserID", user.Id)
                };

                var props = new AuthenticationProperties(new Dictionary<string, string>
                    {
                        {
                            "surname", "Smith"
                        },
                        {
                            "age", "20"
                        },
                        {
                        "gender", "Male"
                        }
                    });

                ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);
                //context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));
                var ticket = new AuthenticationTicket(oAutIdentity, props);
                context.Validated(ticket);

            }
            else
            {
                context.SetError("invalid_grant", "Error");
            }
        });
    }

我的大部分代码都来自此站点。 https://olepetterdahlmann.com/2016/08/08/implement-an-oauth-2-0-authorization-server-using-owin-oauth-middleware-on-asp-net-web-api/

c# asp.net-web-api oauth-2.0 owin
1个回答
0
投票

是,您可以通过以下方式进行操作:

© www.soinside.com 2019 - 2024. All rights reserved.