[英]Authenticating custom Umbraco controller with OWIN/OIDC against Azure AD B2C
我被卡住了。
我有一個自定義的Umbraco控制器,它繼承自RenderMvcController
,當命中Index()
動作方法時,返回一個文件。 這是有效的,但我想要做的是通過使用AuthorizeAttribute
修飾操作然后要求用戶進行身份驗證來保護它。
namespace MyNamespace.Controllers
{
public class MyModelController : RenderMvcController
{
[Authorize]
public ActionResult Index(RenderModel model)
{
// ...
}
}
}
使用OWIN和OpenId Connect對Azure AD B2C應用程序進行身份驗證。 這也有效並且經過測試,但是在非Umbraco環境中。
我已經閱讀了很多與該主題相關的線程和代碼,但我正在努力將其集成到Umbraco中。 我有一個繼承自UmbracoDefaultOwinStartup
的自定義啟動類。 我向AuthController
注冊了一個自定義路由,並通過IAppBuilder.UseOpenIdConnectAuthentication()
配置OIDC。
但我需要Umbraco膠水,並且在理解我應該如何配置cookie時遇到問題。 我已經檢查過調用了啟動Configuration()
方法。
namespace MyNamespace
{
public class CustomOwinStartup : UmbracoDefaultOwinStartup
{
public override void Configuration(IAppBuilder app)
{
base.Configuration(app);
ConfigureAuth(app);
RouteTable.Routes.MapRoute(
"CustomAuth",
"CustomAuth/{action}",
new { controller = "Auth" }
);
}
private void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/CustomAuth/SignUpSignIn") // TODO: What should I put here?
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseOpenIdConnectAuthentication(
// Passing options that are tested and working
);
}
}
}
然后我有我的auth控制器,暫時非常簡單。 我沒有要求將身份驗證信息與Umbraco用戶同步。
namespace MyNamespace.Controllers
{
public partial class CustomAuthController : Controller
{
public CustomAuthController() : base()
{
}
public void SignUpSignIn()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge();
return;
}
Response.Redirect("/"); // TODO: Maybe this should redirect me back to original route MyModel/Index in some way
}
}
}
如果我運行這個並嘗試通過我的屬性裝飾自定義Umbraco控制器我得到此錯誤:
網頁未找到
沒有umbraco文檔匹配網址
'/login.aspx?ReturnUrl=MYORIGINALROUTEHTTPENCODED'
。 此頁面可以替換為自定義404.請查看“自定義404”的文檔。
我的猜測是,這是因為Web.config
中的<authentication mode="Forms">
設置,但是如果我刪除它或將屬性mode
設置為"None"
,這是否會影響后台登錄?
非常感謝有人能幫我指出正確的方向!
不幸的是,我沒有足夠的時間來設置相同的環境並檢查所有內容,但我會給出幾個想法並發布幾個鏈接。
要覆蓋后台登錄邏輯,您無需創建自定義身份驗證控制器或覆蓋登錄頁面。 由於Umbraco使用ASP.NET Identity進行授權,因此您需要在OWIN啟動時正確配置它,它將根據您的需要運行。 然后,您將使用Umbraco授權工具來檢查用戶是否可以點擊MyModelController的Index操作。
如果您需要實現自定義用戶名/密碼,請使用Umbraco擴展。 查看更多: https : //our.umbraco.org/documentation/Reference/Security/#replacing-the-basic-username-password-check
您也可以將Umbraco擴展名用於Active Directory。 查看更多: https : //our.umbraco.org/documentation/Reference/Security/#authenticating-with-active-directory-credentials
關於AuthorizeAttribute。 你找不到錯誤導致授權對OWIN和Umbraco一無所知。 它重定向到默認路徑,即login.aspx頁面,它在項目中不存在。 您可以將<authentication mode="Forms">
為None
。 它不會影響Umbraco后台,因為它使用ASP.NET身份。 如果要保護某些資源,可以使用UmbracoAuthorizeAttribute。
查看更多: https : //our.umbraco.org/documentation/Implementation/Controllers/#mvc
無論如何閱讀更多Umbraco教程和文檔。 我希望你能找到答案。 隨意問更多。
更新
嘗試使用您自己的AuthorizeAttribute版本。 有一個例子: http : //www.c-sharpcorner.com/UploadFile/56fb14/custom-authorization-in-mvc/
您需要覆蓋AuthorizeCore方法並從HandleUnauthorizedRequest返回所需的結果。 因此,您可以將RedirectResult返回到您的登錄頁面或任何您需要的頁面。
您可以將其應用為
[CustomAuthorize]
public ActionResult Index(RenderModel model)
{
// ...
}
更新2
如果自定義AuthorizeAttribute沒有幫助或它不起作用,您可以創建ActionFilter並將授權邏輯放在那里。 當然,這不是最佳實踐,但你至少可以嘗試這樣做。 請看這里: https : //msdn.microsoft.com/en-us/library/gg416513(vs.98).aspx#Anchor_2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.