Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查用户是否在MVC中经过身份验证_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何检查用户是否在MVC中经过身份验证

C# 如何检查用户是否在MVC中经过身份验证,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我已经完成了自定义身份验证系统,它可以正常工作。我的代码如下。我想知道我应该如何控制用户是否在其他操作中进行了身份验证,比如用户是否转到/Profile/Index 我尝试了HttpContext.User和User.Identity,但没有成功 [HttpPost] public ActionResult Login(string username, string password) { if (new UserManager().IsValid(username, password)

我已经完成了自定义身份验证系统,它可以正常工作。我的代码如下。我想知道我应该如何控制用户是否在其他操作中进行了身份验证,比如用户是否转到/Profile/Index

我尝试了HttpContext.User和User.Identity,但没有成功

[HttpPost]
public ActionResult Login(string username, string password)
{
    if (new UserManager().IsValid(username, password))
    {
        var ident = new ClaimsIdentity(
          new[] {
      new Claim(ClaimTypes.NameIdentifier, username),
      new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
      new Claim(ClaimTypes.Name,username)
          },
          DefaultAuthenticationTypes.ApplicationCookie);

        HttpContext.GetOwinContext().Authentication.SignIn(
           new AuthenticationProperties { IsPersistent = false }, ident);
        return RedirectToAction("MyAction"); // auth succeed 
    }
    ModelState.AddModelError("", "invalid username or password");
    return View();
}
这是我的Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

您没有在Owin管道中设置身份验证。最简单的方法是添加一个如下所示的文件。调用它
IdentityConfig.cs
并将其放入
App_Start
文件夹:

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;

//This line tells Owin which method to call
[assembly: OwinStartup(typeof(TokenBasedAuthenticationSample.IdentityConfig))]
namespace TokenBasedAuthenticationSample
{
    public class IdentityConfig
    {
        public void Configuration(IAppBuilder app)
        {
            //Here we add cookie authentication middleware to the pipeline 
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/login"),
            });
        }
    }
}

登录并移动到/Profile/Index后,User.Identity.IsAuthenticated返回什么?您是否尝试过
HttpContext.GetOwinContext().Authentication.User.Identity.IsAuthenticated
?@DavidG,它返回什么false@AliBaig,User.Identity.IsAuthenticated也返回false,在Profile controller上,它通过[Authorize]属性,但在IsAuthenticated时返回false?它找不到UseCookieAuthentication是否使用上述语句添加了
?您是否安装了
Microsoft.Owin.Security.Cookies
软件包?我添加了它,但当它转到MyAction时,声明似乎为空。该操作是否具有
授权
属性?不,我没有,但我只是添加了它。现在,它转到默认登录页面。