2015-01-15 55 views
1

我開發了一個模塊,它動態地從數據庫授權角色。現在,我想要的是,當用戶來到並瀏覽不同的actionmethod而不登錄時,我可以將用戶重定向到登錄頁面。只要用戶登錄,他就應該重定向到他未嘗試登錄時嘗試訪問的actionmethod/view。以下是我正在使用的代碼來提取未登錄瀏覽的URL。我的web.config中還有一個定義爲serverURL的鍵,它給了我最初的URL,如localhost。如何我讓下面RETURNURL記住並登錄後用戶重定向到所需actionmethod /視圖登錄後(認證)重定向到瀏覽的網址

returnUrl = HttpContext.Current.Request.RawUrl; 


public class AuthorizeUserAttribute : AuthorizeAttribute 
    { 
     public string Feature { get; set; } 
     public string returnUrl { get; set; } 

     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      //var isAuthorized = base.AuthorizeCore(httpContext); 
      //if (!isAuthorized) 
      //{ 
      // return false; 
      //} 

      if (httpContext != null && httpContext.Session != null && httpContext.Session["Role"] != null) 
      { 
       string userRoles = UserBL.ValidateUsersRoleFeature(httpContext.Session["Role"].ToString(), Feature); 
       if (!string.IsNullOrEmpty(userRoles)) 
       { 
        if (userRoles.IndexOf(httpContext.Session["Role"].ToString()) >= 0) 
        { 
         return true; 
        } 
       } 
       return false; 
      } 
      else 
       return false; 
     } 


     public override void OnAuthorization(AuthorizationContext filterContext) 
     { 
      HttpSessionStateBase session = filterContext.HttpContext.Session; 

      if (session.IsNewSession || session["Email"] == null) 
      { 
       if (filterContext.HttpContext.Request.IsAjaxRequest()) 
       { 
        // For AJAX requests, return result as a simple string, 
        // and inform calling JavaScript code that a user should be redirected. 
        JsonResult result = new JsonResult(); 
        result.ContentType = "text/html"; 
        result.Data = "SessionTimeout"; 
        filterContext.Result = result; 

        //$.ajax({ 
        // type: "POST", 
        // url: "controller/action", 
        // contentType: "application/json; charset=utf-8", 
        // dataType: "json", 
        // data: JSON.stringify(data), 
        // async: true, 
        // complete: function (xhr, status) { 
        //   if (xhr.responseJSON == CONST_SESSIONTIMEOUT) { 
        //    RedirectToLogin(true); 
        //    return false; 
        //   } 
        //   if (status == 'error' || !xhr.responseText) { 
        //    alert(xhr.statusText); 
        //   } 
        //  } 
        // }); 
        //} 
       } 
       else 
       { 
        // For round-trip requests, 
        filterContext.Result = new RedirectToRouteResult(
         new RouteValueDictionary { { "Controller", "User" }, { "Action", "Login" } }); 

        returnUrl = HttpContext.Current.Request.RawUrl; 

       } 
      } 
      else 
       base.OnAuthorization(filterContext); 
     } 

     protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
     { 
      filterContext.Result = new RedirectToRouteResult(
         new RouteValueDictionary(
          new 
          { 
           controller = "Base", 
           action = "PageNotAccessible" 
          }) 
         ); 
     } 
    } 

回答

0

在屬性返回哪個用戶在路線的網址:。

filterContext.Result = new RedirectToRouteResult(
          new RouteValueDictionary 
            { 
            { "Controller", "User" }, 
            { "Action", "Login" }, 
            {"returnUrl",HttpContext.Current.Request.RawUrl} 
            }); 

,並在你的行動:

[AllowAnonymous] 
public virtual ActionResult Login() 
{ 
    ViewBag.returnUrl = Request.QueryString["returnUrl"]; 
    return View(); 
} 

在View:

@using(Html.BeginForm("Login","User",new{returnUrl = ViewBag.returnUrl},FormMethod.Post)) 
{ 
<input type="submit" value="Login" /> 
} 

,並在後期操作:

[AllowAnonymous] 
[HttpPost] 
public virtual ActionResult Login(User model, string returnUrl) 
{ 
    if(ModelState.IsValid) 
    { 
     // check if login successful redirect to url from where user came 
     if(LoginSucessful) 
     return Redirect(returnUrl); // will be redirected to url from where user came to login 

    return View(); 
} 
0
在HTML頁面中

,創建一個隱藏的標籤:

<div id="HiddenURL" class="hidden"></div> 

當下用戶訪問特定頁面,使用JavaScript的用戶來源網址綁定在你網頁中隱藏的價值:

$(document).ready(function() 
{ 
$('#HiddenURL').text(window.location.href.toLowerCase()); 

... 
} 

在你的asp.net頁面分配到行動,從DIV文本訪問的網址:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
     { 
      filterContext.Result = new RedirectToRouteResult(
         new RouteValueDictionary(
          new 
          { 
           controller = "Base", 
           action = HiddenURL.Value 
          }) 
         ); 
     } 
相關問題