我为api和ui创建了两个项目。我使用过滤器在api中进行了身份验证
namespace EIS.WebAPI.Filters
{
public class Authorization : AuthorizeAttribute,IAuthorizationFilter
{
public readonly IDistributedCache distributedCache;
public Authorization(IDistributedCache _distributedCache)
{
distributedCache = _distributedCache;
}
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
//Authentication
bool skipAuthorization = filterContext.Filters.Any(item => item is IAllowAnonymousFilter);
if (skipAuthorization)
{
return;
}
try
{
string token = distributedCache.GetString("TokenValue");
if (token==null)
{
// unauthorized!
filterContext.Result = new UnauthorizedResult();
}
}
catch (InvalidOperationException)
{
filterContext.Result = new UnauthorizedResult();
}
}
}
}
如果结果未经授权,我想重定向到UI项目的登录页面。 那么有什么方法可以处理UI项目中的api响应。