asp.net mvc - Take controller attribute into account from an OWIN authentication middleware -


i in process of putting custom owin authentication middleware in order re-use our central authentication mechanism.

in order control access pages, using [authorize] , [allowanonymous] attributes on controllers.

even though understand the owin middleware , attribute @ different stages of page life cycle, wondering if there way notify middleware allowanonymous attribute present , there no need process request further.

typically, having middleware trigger authentication process (in case, implying go remote page - oauth2 type of authentication) issue when accessing entry page of site supposed accessible anonymously.

do know way accomplish that?

i know old, had same issue. in case haven't found answer, others come looking:

the thing remember middleware handle request before controller gets , response after. so, don't need access attributes directly, need results.

consider following:

namespace customauthapp.mvc {     public partial class startup     {         public void configureauth(iappbuilder app)         {             app.use(async (ctx, next) =>              {                 if (ctx.extensionmethodtocheckifaccesstokenexistsinrequestmaybeasacookie())                  {                     var ident = new claimsidentity("external");                     ctx.request.user = new claimsprincipal(ident);                 }                  await next();                  if (ctx.response.statuscode == 401)                 {                     var loginuri = string.format("{0}?returnurl={1}", "/account/login", ctx.request.path);                     ctx.response.redirect(loginuri);                 }             }         });     } } 

we check if access token oauth service exists , if so, set claimsprincipal on request (which make bypass [authorized] attribute). after request handled await next() can check response status , set whatever redirects required.

obviously, extremely simple case , doesn't take things roles or claims account, should down road.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -