ASP.Net Framework에서 ASP.Net Core로 이동 중입니다.ASP.Net 코어에서 AuthorizeAttribute를 무시하고 Json 상태에 응답하십시오.
웹 API이 프로젝트와 ASP.Net 프레임 워크에서,이 같은 AuthorizeAttribute을 사용자 정의 할 수 있습니다
public class ApiAuthorizeAttribute : AuthorizationFilterAttribute
{
#region Methods
/// <summary>
/// Override authorization event to do custom authorization.
/// </summary>
/// <param name="httpActionContext"></param>
public override void OnAuthorization(HttpActionContext httpActionContext)
{
// Retrieve email and password.
var accountEmail =
httpActionContext.Request.Headers.Where(
x =>
!string.IsNullOrEmpty(x.Key) &&
x.Key.Equals("Email"))
.Select(x => x.Value.FirstOrDefault())
.FirstOrDefault();
// Retrieve account password.
var accountPassword =
httpActionContext.Request.Headers.Where(
x =>
!string.IsNullOrEmpty(x.Key) &&
x.Key.Equals("Password"))
.Select(x => x.Value.FirstOrDefault()).FirstOrDefault();
// Account view model construction.
var filterAccountViewModel = new FilterAccountViewModel();
filterAccountViewModel.Email = accountEmail;
filterAccountViewModel.Password = accountPassword;
filterAccountViewModel.EmailComparision = TextComparision.Equal;
filterAccountViewModel.PasswordComparision = TextComparision.Equal;
// Find the account.
var account = RepositoryAccount.FindAccount(filterAccountViewModel);
// Account is not found.
if (account == null)
{
// Treat the account as unthorized.
httpActionContext.Response = httpActionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
return;
}
// Role is not defined which means the request is allowed.
if (_roles == null)
return;
// Role is not allowed
if (!_roles.Any(x => x == account.Role))
{
// Treat the account as unthorized.
httpActionContext.Response = httpActionContext.Request.CreateResponse(HttpStatusCode.Forbidden);
return;
}
// Store the requester information in action argument.
httpActionContext.ActionArguments["Account"] = account;
}
#endregion
#region Properties
/// <summary>
/// Repository which provides function to access account database.
/// </summary>
public IRepositoryAccount RepositoryAccount { get; set; }
/// <summary>
/// Which role can be allowed to access server.
/// </summary>
private readonly byte[] _roles;
#endregion
#region Constructor
/// <summary>
/// Initialize instance with default settings.
/// </summary>
public ApiAuthorizeAttribute()
{
}
/// <summary>
/// Initialize instance with allowed role.
/// </summary>
/// <param name="roles"></param>
public ApiAuthorizeAttribute(byte[] roles)
{
_roles = roles;
}
#endregion
}
을 내 사용자 정의 AuthorizeAttribute에서, 내가 계정이 유효한지 여부를 확인 여부 및 클라이언트에 메시지를 HttpStatusCode를 반환 할 수 있습니다 .
ASP.Net Core에서 아무 것도하지 않으려 고하지만 OnAuthorization을 사용하지 않아도됩니다.
ASP.Net Framework에서와 동일한 작업을 어떻게 수행 할 수 있습니까?
감사합니다.
내가 헤더에서 access_token은을 얻기 위해 HttpRequest를 구문 분석하려면? 왜냐하면 나는 GitHub OAuth, Google OAuth 및 Facebook OAuth를 내 스스로 구현하려고하기 때문입니다. 타사 라이브러리는 사용하기 복잡합니다. – Redplane
문제가되지 않습니다.이 코드를 설정하고'AuthorizationHandlerContext context' 변수를 검사하십시오. 헤더와 루타타 (routedata)가있는'HttpRequest'가 있음을 알게 될 것입니다. –
실제로 HttpRequest가 보이지 않습니다. 여기에 내 이미지가 있습니다 : http://www.mediafire.com/view/c1bv86ktb16qqjj/Screenshot_%2815%29.png – Redplane