C# OAuth2WebAPI附加标记
有没有办法在OAuth的json结果上添加额外的标记?现在我得到了这个结果C# OAuth2WebAPI附加标记,c#,asp.net-web-api,oauth-2.0,owin,C#,Asp.net Web Api,Oauth 2.0,Owin,有没有办法在OAuth的json结果上添加额外的标记?现在我得到了这个结果 { "access_token": "...", "token_type": "bearer", "expires_in": 3599 } 我需要补充的是 ".expires": "...", ".issued": "..." 以下是我的代码片段: public override Task GrantResourceOwnerCredentials(OAuthGrantResourceO
{
"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");
}
});
}
公共覆盖任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
返回Task.Factory.StartNew(()=>
{
var username=context.username;
var password=context.password;
var userService=new userService();
User=userService.GetUserByCredentials(用户名、密码);
如果(用户!=null)
{
var索赔=新列表()
{
新索赔(ClaimTypes.Name、user.Name),
新声明(“UserID”,user.Id)
};
ClaimsIdentity oAutIdentity=新的ClaimsIdentity(claims,Startup.OAuthOptions.AuthenticationType);
已验证(新的AuthenticationTicket(OAUTID实体,新的AuthenticationProperties(){}));
}
其他的
{
SetError(“无效的授权”,“错误”);
}
});
}
此外,它不会显示其他属性,它应该返回姓氏、年龄、性别作为我的样本数据
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");
}
});
}
公共覆盖任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
返回Task.Factory.StartNew(()=>
{
var username=context.username;
var password=context.password;
var userService=new userService();
User=userService.GetUserByCredentials(用户名、密码);
如果(用户!=null)
{
var索赔=新列表()
{
新索赔(ClaimTypes.Name、user.Name),
新声明(“UserID”,user.Id)
};
var props=新的AuthenticationProperties(新字典
{
{
“姓”,“史密斯”
},
{
“年龄”、“20岁”
},
{
“性别”、“男性”
}
});
ClaimsIdentity oAutIdentity=新的ClaimsIdentity(claims,Startup.OAuthOptions.AuthenticationType);
//已验证(新的AuthenticationTicket(OAUTID实体,新的AuthenticationProperties(){}));
var票证=新的身份验证票证(OAUTID实体、道具);
上下文。已验证(票证);
}
其他的
{
SetError(“无效的授权”,“错误”);
}
});
}
我的大部分代码都来自这个网站 是的,您可以通过以下方式进行:
var customProperties = new Dictionary<string, string>();
customProperties.Add("as:client_id", context.ClientId ?? string.Empty);
customProperties.Add("user_roles", user_roles);
customProperties.Add("user_timezone", timeZoneName);
customProperties.Add("user_country", account.CountryCode ?? string.Empty);
AuthenticationProperties properties = new AuthenticationProperties(customProperties);
AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
context.Validated(ticket);
var customProperties=new Dictionary();
添加(“as:client_id”,context.ClientId??string.Empty);
添加(“用户角色”,用户角色);
添加(“用户\时区”,时区名称);
customProperties.Add(“user\u country”,account.CountryCode??string.Empty);
AuthenticationProperties=新的AuthenticationProperties(customProperties);
AuthenticationTicket=新的AuthenticationTicket(标识、属性);
上下文。已验证(票证);
谢谢