在第一个例子中: -
我正在为变量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}}