简体   繁体   English

如何检查用户是否在MVC中通过了身份验证

[英]How to check if a user is authenticated in MVC

I have done custom authentication system by following this example and it works. 通过遵循此示例 ,我已经完成了自定义身份验证系统,并且它可以工作。 my code is like below. 我的代码如下。 I am wondering how I should control if the user is authenticated in other Actions, lets say if users goes to /Profile/Index? 我想知道如何控制用户是否在其他操作中通过了身份验证,可以说用户是否转到/ Profile / Index?

I have tried HttpContext.User and User.Identity but didnt work. 我尝试了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();
}

this is my Global.asax 这是我的Global.asax

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

You are not setting up authentication in your Owin pipeline. 您没有在Owin管道中设置身份验证。 The easiest way is to add a file like the one below. 最简单的方法是添加一个类似于以下文件的文件。 Call it IdentityConfig.cs and put it in the App_Start folder: 将其App_StartIdentityConfig.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"),
            });
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何检查用户是否在 MVC5 中通过了身份验证? - How should I check if a user is authenticated in MVC5? 如何检查ASP.NET MVC用户是否从单独的WCF服务应用程序进行了身份验证和授权? - How to check an ASP.NET MVC user is authenticated and authorized from a separate WCF service application? 如何在Web API 2中检查用户是否已通过身份验证 - How to check if user is authenticated in Web API 2 如果用户通过 Claims 的身份验证,如何签入 sharepoint? - How to check in sharepoint if user is authenticated by Claims? MVC C#检查是否对ajax请求进行了身份验证 - MVC C# check if user is authenticated on ajax requests MVC在经过身份验证的页面上显示已验证的用户信息 - MVC Display Authenticated User Information on Authenticated pages 在 mvc 中进行身份验证并重定向到 silverlight,如何访问经过身份验证的用户? - authenticate in mvc and redirect to silverlight, how to access authenticated user? 检查是否已在IdentityServer 4中对用户进行身份验证 - Check if user is already authenticated in IdentityServer 4 如何检查用户是否在Sitefinity小部件模板中登录(已验证)? - How to check if user is logged in (authenticated) in Sitefinity widget template? 如何检查用户是否在 ASP.NET Core 中通过身份验证 - How to check if a user is authenticated in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM