AuthenticationProperties 在基于令牌的身份验证中不起作用
AuthenticationProperties not working in Token based authentication
我在我的项目中使用基于 OAuth Bearer 令牌的身份验证。成功登录请求后,我收到以下 json.
{"access_token":"some token","token_type":"bearer","expires_in":1232}
我想在下方发送更多信息数据json。我创建了身份验证票并添加了身份验证属性。但这并非不起作用。
GrantResourceOwnerCredentials 方法代码:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
try
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var schoolId = context.UserName;
var password = context.Password;
logger.InfoFormat(CommonConstants.LoginInfoLogMessageFormat, schoolId);
var loginOperator = new LoginManager();
var result = loginOperator.IsUser(schoolId, password);
if (result)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
var authenticationProperties = GetUserAuthenticationProperties();
var authenticationTicket = new AuthenticationTicket(identity, authenticationProperties);
context.Validated(authenticationTicket);
}
else
{
context.SetError("invalid_grant", "Kullanıcı adı veya şifre yanlış.");
}
}
catch (Exception exception)
{
logger.ErrorFormat("An error occured GrantResourceOwnerCredentials() method: {0}", exception);
}
}
GetUserAuthenticationProperties 方法代码:
private AuthenticationProperties GetUserAuthenticationProperties()
{
IDictionary<string, string> authenticationInformation = new Dictionary<string, string>();
authenticationInformation.Add("batuhan", "avlayan");
authenticationInformation.Add("fuat", "bugra");
return new AuthenticationProperties(authenticationInformation);
}
覆盖 TokenEndpoint 方法。
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 override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
context.AdditionalResponseParameters.Add("displayname", displayName);
return Task.FromResult<object>(null);
}
我在我的项目中使用基于 OAuth Bearer 令牌的身份验证。成功登录请求后,我收到以下 json.
{"access_token":"some token","token_type":"bearer","expires_in":1232}
我想在下方发送更多信息数据json。我创建了身份验证票并添加了身份验证属性。但这并非不起作用。
GrantResourceOwnerCredentials 方法代码:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
try
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var schoolId = context.UserName;
var password = context.Password;
logger.InfoFormat(CommonConstants.LoginInfoLogMessageFormat, schoolId);
var loginOperator = new LoginManager();
var result = loginOperator.IsUser(schoolId, password);
if (result)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
var authenticationProperties = GetUserAuthenticationProperties();
var authenticationTicket = new AuthenticationTicket(identity, authenticationProperties);
context.Validated(authenticationTicket);
}
else
{
context.SetError("invalid_grant", "Kullanıcı adı veya şifre yanlış.");
}
}
catch (Exception exception)
{
logger.ErrorFormat("An error occured GrantResourceOwnerCredentials() method: {0}", exception);
}
}
GetUserAuthenticationProperties 方法代码:
private AuthenticationProperties GetUserAuthenticationProperties()
{
IDictionary<string, string> authenticationInformation = new Dictionary<string, string>();
authenticationInformation.Add("batuhan", "avlayan");
authenticationInformation.Add("fuat", "bugra");
return new AuthenticationProperties(authenticationInformation);
}
覆盖 TokenEndpoint 方法。
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 override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
context.AdditionalResponseParameters.Add("displayname", displayName);
return Task.FromResult<object>(null);
}