7

ForgetPassword という名前のアクションがあります。匿名ユーザーがアクションを取得しようとするたびに、ログイン ページにリダイレクトされます。以下は私の実装です。

public ActionResult ForgotPassword(string UserName)
{
    //More over when i place a breakpoint for the below line 
    //its not even getting here
    return View("Login");
}

そして、これが私のweb.configファイルの一部です

    <location path="">
        <system.web>
          <authorization>
            <deny users="?"/>
          </authorization>
        </system.web>    
      </location>

  <location path="Content">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>    
  </location>

  <location path="Scripts">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>    
  </location>

  <location path="Images">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>    
  </location>

<authentication mode="Forms">
  <forms loginUrl="/Home/Login" timeout="5" slidingExpiration="false" />
</authentication>
4

4 に答える 4

9

を使用してアプリケーションからすべてを拒否しているように。

<authorization>
    <deny users="?"/>
</authorization>

IMHO、アプリケーションの認証を制御するために web.config を使用しないでください。代わりにAuthorize属性を使用してください。

メソッドGlobal.asaxの下のファイルにこれを追加しますRegisterGlobalFilters

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new AuthorizeAttribute()); //Added
}

または、コントローラーも飾ることができます[Authorize]

[Authorize]
public class HomeController : Controller
{
    ...
}

ASP.NET MVC4 を使用している場合、匿名アクセスが必要なアクションにはAllowAnonymous属性を使用します

[AllowAnonymous]
public ActionResult ForgotPassword() {
    //More over when i place a breakpoint for the below line 
    //its not even getting here
    return View("Login");;   
}

参照に従って、ルーティングまたは web.config ファイルを使用して MVC アプリケーションを保護することはできません。MVC アプリケーションを保護するためにサポートされている唯一の方法は、Authorize 属性を各コントローラーに適用し、ログインおよび登録アクションで新しい AllowAnonymous 属性を使用することです。現在の領域に基づいてセキュリティの決定を下すことは非常に悪いことであり、アプリケーションを脆弱性にさらすことになります.

于 2013-07-10T11:15:20.250 に答える
3

このリンクから: http://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous

MVC 3 を使用している場合、次のことはできません。

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new AuthorizeAttribute());
}

グローバルであり、AllowAnonymous属性がMVC 3で機能しない理由。

したがって、独自のフィルターを作成する必要があります。それは私のために働いています(MVC 3) 。ここで完全なソリューションを確認できます。

using System.Web.Mvc;
using MvcGlobalAuthorize.Controllers;

namespace MvcGlobalAuthorize.Filters {
    public sealed class LogonAuthorize : AuthorizeAttribute {
        public override void OnAuthorization(AuthorizationContext filterContext)         {
            bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
            || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
            if (!skipAuthorization) {
                base.OnAuthorization(filterContext);
            }
        }
    }
}
于 2015-04-30T21:26:49.060 に答える
0

ASP.NET MVC4 を使用している場合は、次のようにアクションに allowanonymous 属性を設定できます。

[AllowAnonymous]
public ActionResult ForgotPassword(string UserName)
{
    //More over when i place a breakpoint for the below line 
    //its not even getting here
    return View("Login");
}

詳細については、Jon Galloway の記事をご覧ください: Global authentication and Allow Anonymous

于 2013-07-10T11:05:11.093 に答える