Asp.net Identity 2.0自定义登录方法

潘诺夫

我正在使用Identity 2.0开发ASP.NET 5应用程序。我有两种类型的用户:

  1. 正常-他们使用标准的登录方法进行身份验证。
  2. 临时-他们应根据提供的令牌登录。

除了验证用户所需的信息(一些用户名和令牌)之外,我不想存储临时用户。如果用户提供用户名和有效密码,则应登录。

我不确定如何实现这一目标。

山姆·法拉格(Sam Farajpour Ghamari)

您也可以同时在两种情况下使用身份。对于第一种情况,使用Identity就像您之前所做的一样,没有任何更改,但是对于第二种情况,您需要稍微修改登录方法。

public ActionResoult TempLogin(string username, string password)
{
    // imaging you have own temp user manager, completely independent from identity
    if(_tempUserManager.IsValid(username,password))         
    {
        // user is valid, going to authenticate user for my App
        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"),

            // an optional claim you could omit this 
            new Claim(ClaimTypes.Name, username),

            // you could even add some role
            new Claim(ClaimTypes.Role, "TempUser"),
            new Claim(ClaimTypes.Role, "AnotherRole"),
            // and so on
        },
        DefaultAuthenticationTypes.ApplicationCookie);

        // Identity is sign in user based on claim don't matter 
        // how you generated it Identity 
        HttpContext.GetOwinContext().Authentication.SignIn(
            new AuthenticationProperties { IsPersistent = false }, ident);

        // auth is succeed, 
        return RedirectToAction("MyAction"); 
     }
     ModelState.AddModelError("", "We could not authorize you :(");
     return View();
}

由于我们将逻辑注入到Identity中,因此我们根本不需要做任何额外的事情。

[Authorize]
public ActionResult MySecretAction()
{
    // all authorized users could use this method don't matter how has been authenticated
    // we have access current user principal by calling also
    // HttpContext.User
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何自定义Asp.net Identity 2用户名已采取的验证消息?

ASP.Net Identity 2-来自OAuthAuthorizationServerProvider的自定义响应

使用自定义Facebook和Google按钮的ASP.NET Identity登录

使用ASP.NET Identity 2.0和MVC 5的自定义单点登录

ASP.NET Identity与自定义实现,使用哪个?

ASP .NET Core Identity 自定义 ApiAuthorizationDbContext

已验证ASP.NET Identity中的自定义登录规则,但浏览器仍重定向到“主页”

ASP.Net Identity 2 RemovePassword模拟

创建编程用户后无法登录ASP.NET Identity 2网站

使用 Google 登录的 ASP.NET Identity 2...注销不起作用

基于用户角色的ASP.NET MVC 5 Identity 2登录重定向

ASP.NET Core-自定义AspNetCore.Identity实现不起作用

Asp .Net Identity 自定义 AspNetUser 主 ID 字段值?

使用ASP.NET Identity 3的自定义密码策略

在MVC 5中的ASP.NET Identity 3上管理自定义用户属性

将自定义列添加到ASP.NET Identity

使用自定义 List<property> 扩展 ASP .NET Identity 并在视图中访问它

Asp.net Identity 2.0使用自定义唯一属性扩展UserValidator

自定义MVC5 ASP.NET Identity中的cookie值

使用自定义角色在ASP.NET Identity中初始化RoleManager

如何在ASP.NET IDENTITY中添加自定义表?

在Asp.Net Core 3 Identity中创建自定义SignInManager

如何为 Asp.Net Identity 自定义表名(例如从 AspNetUsers 更改为自定义名称)

在ASP.Net Core中内置具有自定义表功能的ASP.Net Identity

ASP.NET MVC 5 Identity 2 PasswordSignInAsync方法始终返回false

ASP.NET Identity 2层次结构角色

ASP.NET Identity2和DataContext注入

Asp.NET Identity 2提供“无效令牌”错误

ASP.NET Identity 2使身份困难失效