从Mvc中的控制器重定向后,我无法获取User.Identity.Name

时间:2015-03-03 10:50:00

标签: asp.net-mvc asp.net-mvc-4 redirect asp.net-mvc-5 forms-authentication

在第一个例子中: -

我正在为变量id分配User.Identity.Name值。我能够在Redirecting之后获得价值,我在Redirect(ReturnUrl)处使用User.Identity.Name现在我可以在其他controller中获得Redirected值(User.Identity.Name查看)也

但在第二个例子中: -

我正在为变量ID分配Redirecting值我能够在{i} return Redirect(ReturnUrl);之后获取值,而我在使用return Redirect(ReturnUrl);时正在使用User.Identity.Name {1}}无法获得Redirected网址

中的public ActionResult SignIn(string ReturnUrl) { if (ReturnUrl == "/" || string.IsNullOrEmpty(ReturnUrl)) { ReturnUrl = "/Dashboard"; } var id=HttpContext.Current.User.Identity.Name; Response.Redirect(ReturnUrl); return View(); }

示例1: -

public ActionResult SignIn(string ReturnUrl)
 {
    if (ReturnUrl == "/" || string.IsNullOrEmpty(ReturnUrl))
    {
        ReturnUrl = "/Dashboard";
    }
    var id=HttpContext.Current.User.Identity.Name;
    return Redirect(ReturnUrl);
 }

示例2: -

return Redirect(ReturnUrl);

我的控制器: - 如果我是User.Identity.Name,则在此功能中,如果我使用Response.Redirect(ReturnUrl); return View();,则无法获取CompanyRequired过滤器中的User.Identity.Name值,然后才能获得CompanyRequired filter 1}} return Redirect(ReturnUrl);中的值,但我必须使用 [HttpPost] public async Task<ActionResult> SignInCallback() { var token = Request.Form["id_token"]; var state = Request.Form["state"]; var claims = await ValidateIdentityTokenAsync(token, state); string ReturnUrl = state.Substring(state.IndexOf('?') + 1); var id = new ClaimsIdentity(claims, "Cookies"); Request.GetOwinContext().Authentication.SignIn(id); if (ReturnUrl == "/" || string.IsNullOrEmpty(ReturnUrl)) { ReturnUrl = "/Dashboard"; } var Id = User.Identity.Name; return Redirect(ReturnUrl); }

[Authorize,CompanyRequired]
public class DashBoardController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }
}

查看: - 这里我控制将转到CompanyRequired过滤器,我需要一个User.Identity.Name值,因为我得到的值为null

 public class CompanyRequiredAttribute : ActionFilterAttribute
 {
    public override void OnActionExecuting(ActionExecutingContext     filterContext)
    {
        var coCookie = filterContext.HttpContext.Request.Cookies["CoId"];

        if (coCookie == null)
        {
        var Id= HttpContext.Current.User.Identity.Name.Int(); **//here i need to get the value but i am getting null value**
        IdNmList cos = new EmployeeDAL().GetCompany(Id);
            if (cos.Count == 0)
            {
                filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary
                {
                    { "controller" , "Company"},
                    { "action" , "Add"}
                });
            }
            else if (cos.Count == 1)
            {
                filterContext.HttpContext.Response.Cookies.Add(new HttpCookie("CoId", cos[0].Id.ToString()));
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary
                 {
                            { "controller", "Company" },
                            { "action", "Select" },
                            { "ReturnUrl", filterContext.HttpContext.Request.RawUrl }
                 });
            }
        }
        base.OnActionExecuting(filterContext);
    }       
}

公司要求过滤: -

{{1}}

0 个答案:

没有答案