【发布时间】:2016-07-24 17:32:30
【问题描述】:
我需要使用 ASP.NET MVC 3 实现摘要身份验证。为此,我继承了 AuthorizeAttribute 和 HttpUnauthorizedResult。代码如下:
[AttributeUsage ( AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true )]
public class SessionAuthorize: AuthorizeAttribute {
public override void OnAuthorization ( AuthorizationContext actionContext ) {
try {
if ( null != actionContext.HttpContext.Request.Headers["Authorization"] )
// authorization is on the way
// <...>
else
actionContext.Result = new HttpDigestUnauthorizedResult ();
} catch ( Exception ex ) {
Trace.TraceWarning ( "SessionAuthorize.OnAuthorization failed: {0}", ex.Message );
}
base.OnAuthorization ( actionContext );
}
}
public class HttpDigestUnauthorizedResult: HttpUnauthorizedResult {
public HttpDigestUnauthorizedResult () : base () {
}
public override void ExecuteResult ( ControllerContext context ) {
if ( context == null )
throw new ArgumentNullException ( "context" );
// this is supposed to initialize digest authentification exchange
context.HttpContext.Response.AddHeader ( "WWW-Authenticate", string.Format ( "Digest realm=\"somerealm\",qop=\"auth\",nonce=\"{0}\",opaque=\"{1}\""/*, <...>*/ ) );
base.ExecuteResult ( context );
}
}
控制器/动作代码如下:
public class DefaultController: Controller {
[SessionAuthorize]
public ViewResult Index () {
return View ();
}
}
所以它没有做任何特别的事情。
但是,覆盖的ExecuteResult 永远不会被调用,只会返回标准的 401 页面。我在这里想念什么?应该从哪里调用ExecuteResult?
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-3 digest-authentication