Bat*_*yan 1 oauth owin asp.net-web-api2
我在项目中使用基于OAuth Bearer令牌的身份验证.成功登录请求后,我收到以下json.
{"access_token":"some token","token_type":"bearer","expires_in":1232}
Run Code Online (Sandbox Code Playgroud)
我想在json下面发送更多信息数据.我创建了身份验证票证并添加了authenticationproperties.但它并没有起作用.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
GetUserAuthenticationProperties方法代码:
private AuthenticationProperties GetUserAuthenticationProperties()
{
IDictionary<string, string> authenticationInformation = new Dictionary<string, string>();
authenticationInformation.Add("batuhan", "avlayan");
authenticationInformation.Add("fuat", "bugra");
return new AuthenticationProperties(authenticationInformation);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
覆盖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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1635 次 |
| 最近记录: |