我正在设计一个.NET 4.6 SPA(单页面应用程序)。我希望在用户通过身份验证后返回其他参数。
在ApplicationOAuthProvider中,我有以下代码来返回其他参数:
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);
}
public static AuthenticationProperties CreateProperties(Participant user, IEnumerable<string> roles)
{
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "userName", user.UserName },
{ "fullName", user.FullName },
{ "userRoles", string.Join(",",roles) }
};
return new AuthenticationProperties(data);
}
在将JSON响应返回到/ Token的帖子时,这很有效,但是,我遇到了使用在创建.NET 4.6 SPA时自动生成的默认AccountController模板的ExternalLogin Action的问题,但是使用了修改后的ApplicationOAuthProvider .CreateProperties函数定义如上。
bool hasRegistered = user != null;
if (hasRegistered)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user, await UserManager.GetRolesAsync(user.Id));
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
}
...
return Ok();
我认为这会在redirect_uri#之后为片段添加额外的参数,但我只得到3个参数(access_token,expires_in&amp; token_type)。
成功进行外部身份验证后,如何在片段中添加额外参数(或以其他方式获取浏览器窗口中的额外数据)?谢谢。
答案 0 :(得分:0)
最终代码看起来像:
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
...
public override Task AuthorizationEndpointResponse(OAuthAuthorizationEndpointResponseContext context)
{
var props = context.OwinContext.Authentication.AuthenticationResponseGrant.Properties.Dictionary;
foreach (var k in props.Keys)
{
if (k[0] != '.' && !string.Equals(k,"client_id",StringComparison.OrdinalIgnoreCase))
{
context.AdditionalResponseParameters.Add(k, props[k]);
}
}
return base.AuthorizationEndpointResponse(context);
}