2013-05-13 1 views
2

모바일 레이아웃 영역이 있습니다. 나는 또한 정상적인 웹 사이트 레이아웃을 사용하는 내 경로의 컨트롤러도 가지고 있습니다.영역에 따라 다른 로그온 화면 - MVC3

[Authorize (Roles = "ROLENAME")]을 사용하고 사용자가 페이지 (모바일 사이트)가 모바일 사이트가 아닌 일반 웹 사이트 로그인 페이지로 리디렉션되는 역할에 있지 않은 경우 문제가 발생합니다.

사용자가 사이트에 액세스하려고 시도하는 영역에 따라 로그인을 전환 할 수 있습니까?

나는 내 영역의 web.config에 다음을 추가하는 시도했지만 작동하지 않았다

<authentication mode="Forms"> 
     <forms loginUrl="~/Activation/Login/Index" timeout="2880" /> 
</authentication> 

어떤 제안?

답변

2

로그인 할 때 액션이 발생하면 해당 액션이 모바일 장치에 있는지 확인한 다음 모바일 로그인 페이지로 리디렉션됩니다. 그런 다음 액션

private static string[] mobileDevices = new string[] {"iphone","ppc", 
                 "windows ce","blackberry", 
                 "opera mini","mobile","palm", 
                 "portable","opera mobi" }; 

public static bool IsMobileDevice(string userAgent) 
{ 
    // TODO: null check 
    userAgent = userAgent.ToLower(); 
    return mobileDevices.Any(x => userAgent.Contains(x)); 
} 

:

public ActionResult Index() 
{ 
    if (MobileHelper.IsMobileDevice(Request.UserAgent)) 
    { 
     // Send to mobile route. 
     return RedirectToAction("Login", "MobileActivation"); 
    } 

    // Otherwise do normal login 
} 

편집 :이 광범위하게 응용 프로그램이 다음을 수행 할 수를 전체의 적용하고자한다면

.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
public sealed class RedirectToMobileArea : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var rd = filterContext.HttpContext.Request.RequestContext.RouteData; 
     var currentAction = rd.GetRequiredString("action"); 
     var currentController = rd.GetRequiredString("controller"); 
     string currentArea = rd.Values["area"] as string; 

     if (!currentArea.Equals("mobile", StringComparison.OrdinalIgnoreCase) && MobileHelper.IsMobileDevice(Request.UserAgent)) 
     { 
      filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", currentController }, { "action", currentAction }, { "area", "mobile" } }); 
     } 
    } 
} 

이 필터는 경우 조치 점검을 ​​모바일 (그리고 이미 모바일 영역에서)와 같은 행동과 컨트롤러로 전송됩니다

이 같은 어디서든 뭔가를 적용 할 수있는 ActionFilter 만들기 모바일 영역. 참고 당신이 그런 see this answer

당신과 같은 각 작업에 필터를 적용 할 수 중 하나는 컨트롤러 네임 스페이스와 경로를 등록해야합니다 같은 이름의 컨트롤러와 함께 갈 경우 :

[RedirectToMobileArea] 
public ActionResult Index() 
{ 
// do stuff. 
} 

또는 경우

[RedirectToMobileArea] 
public abstract class BaseController : Controller { 

} 

를 다음에 : 당신은 모든 지역 모든 컨트롤러에서 상속하고 그에 적용됩니다 BaseController를 생성하고 싶은 그것에서 헤리 트가 :

+0

덕분에이 내가 갔다하는 방식이었다 ...

public HomeController : BaseController { } 

나는이 모든 테스트 havent 한하지만 작동합니다. 내가 그것을하고 싶었던 방식으로 가능한지 알고 싶었을 것이다. –

+0

하나의 옵션으로 수정 사항을 추가하겠습니다. – shenku

+0

끝내 주셔서 감사합니다! –

관련 문제