我正在開發一個使用ASP.NET Web API的Web服務。我正在使用ASP.NET身份驗證和令牌生成。我需要在令牌響應json中返回一個擴展屬性。直到現在我能夠返回一個擴展的字符串屬性,我在其中發送一個通過將自定義類對象序列化爲json而獲得的json字符串。以下是我的身份驗證提供程序代碼:Web API認證響應屬性
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
await Task.Run(() =>
{
context.Validated();
});
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
await Task.Run(() =>
{
var loginResponse = new AccountManager().Login(context.UserName, context.Password);
if (loginResponse == null)
{
context.SetError("invalid_grant", Resources.Messages.InvalidGrant);
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "userData", JsonConvert.SerializeObject(loginResponse) }
};
AuthenticationProperties properties = new AuthenticationProperties(data);
Microsoft.Owin.Security.AuthenticationTicket ticket = new Microsoft.Owin.Security.AuthenticationTicket(identity, properties);
context.Validated(ticket);
});
}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
return Task.FromResult<object>(null);
}
}
現在在我的回覆中,我有一個屬性,例如"userData" : "<Json String>"
而我想分配一個json對象(不是json字符串)到userData。可能嗎?
試圖準確理解你在做什麼,是否有一個原因,你不能只是建立你的對象,因爲你喜歡它,然後只是使用stringify方法將其轉換爲JSON字符串? – Pseudonym 2014-10-27 13:25:48