Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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# 如何在ASP.NET MVC 5中实现自定义身份验证_C#_Asp.net Mvc_Authentication_Authorization_Asp.net Identity - Fatal编程技术网

C# 如何在ASP.NET MVC 5中实现自定义身份验证

C# 如何在ASP.NET MVC 5中实现自定义身份验证,c#,asp.net-mvc,authentication,authorization,asp.net-identity,C#,Asp.net Mvc,Authentication,Authorization,Asp.net Identity,我正在开发一个ASP.NETMVC5应用程序。我有一个现有的数据库,我从中创建了ADO.NET实体数据模型。 我在数据库中有一个包含“username”和“password”列的表,我想用它们在我的Webapp中实现身份验证和授权;由于客户的要求,我无法创建任何其他数据库、表或列,也无法使用标准身份验证。 我不需要管理注册、密码更改或其他事情:只需使用密码和用户名登录即可。 我该怎么做 是的,你可以。身份验证和授权部分独立工作。如果您有自己的身份验证服务,您可以使用OWIN的授权部分。假设您已经

我正在开发一个ASP.NETMVC5应用程序。我有一个现有的数据库,我从中创建了ADO.NET实体数据模型。 我在数据库中有一个包含“username”和“password”列的表,我想用它们在我的Webapp中实现身份验证和授权;由于客户的要求,我无法创建任何其他数据库、表或列,也无法使用标准身份验证。 我不需要管理注册、密码更改或其他事情:只需使用密码和用户名登录即可。
我该怎么做

是的,你可以。身份验证和授权部分独立工作。如果您有自己的身份验证服务,您可以使用OWIN的授权部分。假设您已经有了<代码>用户管理器< /代码>,它验证了<代码>用户名< /代码>和<代码>密码< /代码>。因此,您可以在回发登录操作中编写以下代码:

[HttpPost]
public ActionResult Login(string username, string password)
{
    if (new UserManager().IsValid(username, password))
    {
        var ident = new ClaimsIdentity(
          new[] { 
              // adding following 2 claim just for supporting default antiforgery provider
              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),

              // optionally you could add roles if any
              new Claim(ClaimTypes.Role, "RoleName"),
              new Claim(ClaimTypes.Role, "AnotherRole"),

          },
          DefaultAuthenticationTypes.ApplicationCookie);

        HttpContext.GetOwinContext().Authentication.SignIn(
           new AuthenticationProperties { IsPersistent = false }, ident);
        return RedirectToAction("MyAction"); // auth succeed 
    }
    // invalid username or password
    ModelState.AddModelError("", "invalid username or password");
    return View();
}
您的用户管理器可以是这样的:

class UserManager
{
    public bool IsValid(string username, string password)
    {
         using(var db=new MyDbContext()) // use your DbConext
         {
             // for the sake of simplicity I use plain text passwords
             // in real world hashing and salting techniques must be implemented   
             return db.Users.Any(u=>u.Username==username 
                 && u.Password==password); 
         }
    }
}
最后,您可以通过添加
Authorize
属性来保护您的操作或控制器

[Authorize]
public ActionResult MySecretAction()
{
    // all authorized users can use this method
    // we have accessed current user principal by calling also
    // HttpContext.User
}

[Authorize(Roles="Admin")]
public ActionResult MySecretAction()
{
    // just Admin users have access to this method
} 

我刚刚更新了我的帖子来回答你的问题。嘿,我想让你知道你的github示例(用于tokenauth)解决了我的问题,非常感谢!如果可以的话,我会给你的例子投票1000次:)需要的nuget软件包:-Microsoft.AspNet.Identity.Core-Microsoft.AspNet.Identity.Owin-Microsoft.Owin-Microsoft.Owin.Host.SystemWeb-Microsoft.Owin.Security-Microsoft.Owin.Security.Cookies-Microsoft.Owin.Security.OAuth-OwinI希望你对这个问题有公开的赏金,这样我们就可以给你+1000。请在博客上的某个地方发布,这样搜索引擎就可以找到这个。它是如此简单和优雅的解决方案。我花了两天时间阅读OWIN和OAuth2提供的内容,但我无法连接电线。@SamFarajpourGhamari:你能解释一下为什么登录时需要长常量字符串吗<代码>。。。新索赔(”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“,”ASP.NET标识“,”http://www.w3.org/2001/XMLSchema#string“我检查了一下,没有它,代码运行得很好!