HttpContext.Current.User.Identity.Name为空,IIS中网站的子文件夹中带有Form.authentication

问题描述 投票:0回答:1

我有一个有3个不同应用程序的场景

  1. 日历视图(Web表单)
  2. Web API(MVC)
  3. MVC应用程序

我已经在一个网站上的IIS上托管了1.和2.。因为我也想在同一网站中包含3.但由于Web api和MVC应用程序包含Global.asax,因此我创建了一个子虚拟目录。

示例:我的网站路径为www.calendarview.com/login.aspx。此应用程序(日历视图(Web表单))使用表单认证。在web.config下面是一个代码

 <system.web>
    <compilation debug="true" targetFramework="4.7.2"/>
    <authentication mode="Forms">
      <forms loginUrl="login.aspx" defaultUrl="Index.aspx" slidingExpiration="true" timeout="2440">
        <credentials passwordFormat="Clear">

Login.aspx

 if (FormsAuthentication.Authenticate(UserName.Text, UserPass.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
                Session["username"] = UserName.Text;
            }
            else
            {
                Msg.Text = "Invalid User Name and/or Password";
            }

登录后

var username = User?.Identity?.Name;
            if (string.IsNullOrWhiteSpace(username))
                FormsAuthentication.RedirectToLoginPage(Request.QueryString.ToString());

现在Web API能够获取HttpContext.Current.User.Identity.Name

=====================================>

现在进入MVC应用程序,其路径类似于www.calendarview.com/Masters /...。

网站包含子文件夹/虚拟目录。在这里,我无法获取HttpContext.Current.User.Identity.Name

MVC应用程序中的代码如下

public class SessionTimeOutAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // strUser = HttpContext.Current.User.Identity.Name.ToString();

            HttpContext ctx = HttpContext.Current;
            if (HttpContext.Current.User.Identity.Name == null || (string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name)))
            {

#if !DEBUG
                filterContext.Result = new RedirectResult(Global.GetLoginURLPath);
#endif
            }
            base.OnActionExecuting(filterContext);

        }
    }

如何使用C#.net在IIS的子文件夹/目录中获取HttpContext.Current.User.Identity.Name

我有一个方案,我有3个不同的应用程序日历视图(Web表单)Web API(MVC)MVC应用程序我已经在一个网站的IIS上托管了1.和2.。因为我也想在相同的位置包含3 ....

c# .net asp.net-mvc api httpcontext
1个回答
0
投票

尝试以下方法以使用户具有ASP.NET标识等。>

© www.soinside.com 2019 - 2024. All rights reserved.