ASP.Net Web API 2 Different Content when authorized -
ASP.Net Web API 2 Different Content when authorized -
i have project that's using asp.net web api 2, , providing oauth back upwards via owin oauth 2 provider. working fine, have nail upon problem when trying differentiate between authenticated users , anonymous users.
i can provide [authorize] , [allowanonymous] attributes, neither quite want do. [authorize] allow authorised users, , [allowanonymous] allow anyone, authenticated or not.
the behaviour i'm looking follows:
user requests endpoint no authorization header in request - anonymous version of endpoint's content returned user requests endpoint valid authorization header in request - logged in version of endpoint's content returned user requests endpoint invalid authorisation header in request - server returns status code of 401i can build custom attribute this, wondering if there way natively without having create custom attribute.
many help can provide.
richard.
update
one alternative i've come create subclass of authorizeattribute this:
public class successfulauthorizationoranonymousattribute : authorizeattribute { protected override bool isauthorized(system.web.http.controllers.httpactioncontext actioncontext) { if(actioncontext.requestcontext.principal.identity.isauthenticated) homecoming base.isauthorized(actioncontext); else { if (actioncontext.request.headers.authorization != null) { // if there's authorization header, there principal.identity.isauthenticated false, authentication must have failed homecoming false; } else { // there's no authorization header - hence user cannot have attempted authenticate. true anonymous request? homecoming true; } } } }
the logic if user comes through attribute authenticated, can allow base of operations class handle whether they're authorised view content.
however, if come through unauthenticated, can check see if there authorisation header in request, , if there think can infer authentication attempted , failed. if that's case can homecoming they're not authorised access resource.
if there's no authorisation header user did not seek authenticate (the api i'm using supports bearer authentication - nil else), , safe treat user anonymous.
from tests, approach seems behave how like, although sense of hack. there reason why shouldn't way can't see?
thanks,
richard.
i not think can in single endpoint accepts anonymous , authenticated users in same time, recommendation create 2 end points , pull business logic private method, below should work you
[allowanonymous] [httppost] [route("actionforanon")] public ihttpactionresult actionforanon() { homecoming doyourlogic(); } [authorize] [httppost] [route("actionforauth")] public ihttpactionresult actionforauth() { homecoming doyourlogic(); } private ihttpactionresult doyourlogic() { var identity = user.identity claimsidentity; if (identity.isauthenticated) { //your response authenticated user } else { //your response anonymous user } homecoming ok(); }
asp.net asp.net-web-api oauth-2.0 owin
Comments
Post a Comment