【问题标题】:OpenID OWIN auth and lack of user permissionsOpenID OWIN 身份验证和缺少用户权限
【发布时间】:2015-05-12 10:23:59
【问题描述】:
【问题讨论】:
标签:
azure
owin
openid-connect
【解决方案1】:
您可以在通知中使用 try...catch,类似于以下内容:
SecurityTokenValidated = (context) =>
{
try
{
// retriever caller data from the incoming principal
var username = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Name).Value.Split('@')[0];
var database = DependencyResolver.Current.GetService(typeof (IDatabase)) as IDatabase;
var employee = database.Query(new GetEmployeeByUsername(username));
if (employee == null)
{
throw new SecurityTokenValidationException();
}
// I add my custom claims here
context.AuthenticationTicket.Identity.AddClaims(claims);
return Task.FromResult(0);
}
catch (SecurityTokenValidationException ex)
{
context.HandleResponse(); // This will skip executing rest of the code in the middleware
context.Response.Redirect(....);
return Task.FromResult(0);
}
}