我正在Azure上構建一個mvc 4 web api項目。請求該API經過的路線:通過IHttpModule通過mvc 4和web api訪問控制服務,網站和服務的不同安全設置?
routes.MapHttpRoute(
name: "DefaultApi", // Route name
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
在同一域和不同的路線,我有一個MVC的Web站點設置:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MVC的網站只是一個HTML和Javascript頁面與訪問控制服務進行交互以獲取令牌,然後將該令牌傳遞給該服務以通過jQuery ajax獲取數據。
要實現服務的安全性(驗證發送到API令牌),我使用的是SWTModule和TokenValidator類作爲這個article描述,在我的MVC 4 Web應用程序項目的Web.Config系統中的呼叫一起。 Web服務器屬性:
<modules runAllManagedModulesForAllRequests="true">
<add name="SWTModule" type="SecurityModule.SWTModule, SecurityModule" />
</modules>
雖然我還沒有打上MVC控制器方法與[授權],加載在這個錯誤的默認網頁結果:
Server Error in '/' Application.
unauthorized
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ApplicationException: unauthorized
Source Error:
Line 43: Line 44: // check that it starts with 'WRAP' Line 45: if (!headerValue.StartsWith("WRAP ")) Line 46:
{ Line 47: throw new ApplicationException("unauthorized");Source File: D:\Dev\VisualStudio2010\Projects\myServices\Azure\myDataInterfaces\SecurityModule\SWTModule.cs Line: 45
Stack Trace:
[ApplicationException: unauthorized]
SecurityModule.SWTModule.context_BeginRequest(Object sender, EventArgs e) in D:\Dev\VisualStudio2010\Projects\myServices\Azure\myDataInterfaces\SecurityModule\SWTModule.cs:45 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270
在我看來,儘管我沒有市場d我的[授權]控制器方法,他們被拒絕了,因爲我的SWT令牌驗證燒製每個響應,大概是因爲它早在網絡堆棧調用,因爲包含在web.config屬性,?
有人能指出我如何使這項工作朝着正確的方向?我想這樣做,以便默認網頁(主控制器索引方法)顯示我的登錄屏幕,然後重定向到主頁面。如果用戶沒有用可用的ACS身份提供者之一登錄,那麼該主頁面應該返回未授權狀態。同樣,我想讓我的一些Web API ApiController方法公開並需要其他人的授權。
目前,我都在MVC應用程序,並在Visual Studio中的單個Web角色的解決方案一個Web應用程序項目中通過一個單一的Global.asax文件路由在Web API服務。我想知道是否解決這個問題的方法是將HTML MVC站點和Web API分離到同一Web Role解決方案下的不同Web應用程序項目中?這仍然不能給我分配一些控制器方法的授權要求,只是給我一個路由控制器和ApiControllers的地方。
我覺得我接近,但也許缺少的東西大了,但我似乎沒有在網上找到它。有人會把我拉直嗎?
謝謝, 亞歷克斯
好的...這是有道理的,它感覺就像我使用堆棧的錯誤部分來做我想做的事,謝謝我會給ThinkTecture文章一個去。 – user483679