如何为Asp.NET MVC 5创建一个ClaimsIdentity对象?

 Max_coffee 发布于 2023-02-12 18:31

我在.NET MVC 5应用程序中工作.我不想使用Entity Framework.我想验证RavenDB数据库.在我看来,我想要替换UserManager帐户控制器附带的.我想我可以重写所有UserManager函数来使用我的数据库,除了我不理解该ClaimsIdentity对象.

在该SignInAsync方法中,有一个调用UserManager.CreateIdentityAsync(...).我知道它会返回一个ClaimsIdentity对象.我不知道的是如何自己创建一个ClaimsIdentity对象.

我看到它有4个属性Actor,BootstrapContext,ClaimsLabel.我不知道这些属性用于什么,我不知道如何正确生成它们.我认为正确生成它们非常重要,因为它是如何生成身份验证cookie的.

我在这里查看了ClaimsIdentity对象的解释,但这并没有真正帮助我理解.

如果我能看到代码CreateIdentityAsync(),那可能会有所帮助.

如果我发现这一切都错了,请告诉我.否则,如果有人可以指出我如何生成ClaimsIdentity对象,那将会有所帮助.

ClaimsIdentity identity = new ClaimsIdentity
{
    Actor = ????,
    BootstrapContext = ?????,
    Claims = ?????,
    Label = ?????
}

Vlince.. 43

也许以下链接可以提供帮助:

var claims = new List();
claims.Add(new Claim(ClaimTypes.Name, "Brock"));
claims.Add(new Claim(ClaimTypes.Email, "brockallen@gmail.com"));
var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);

var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);


Jeff Walker .. 20

我认为你"正在做这件事".ASP.NET Identity Framework在设计时考虑了可插入的持久性.将RavenDB替换为EF的正确方法不是替换UserManager.相反,它是实现替代UserStore.因此AccountController,创建的线UserManager将改变:

public AccountController()
    : this(new UserManager(new UserStore(new ApplicationDbContext())))

至:

public AccountController()
    : this(new UserManager(new RavenDBUserStore/* connection info here*/)))

哪个是RavenDBUserStore您编写的实现类IUserStore,IUserPasswordStore以及您的特定应用程序所需的任何其他*Store接口.

这种方法避免了您需要理解并重新实现所有内容,UserManager并确保您能够利用UserManagerMS 的未来改进.

有关如何执行此操作的详细信息,请参阅ASP.NET标识的自定义存储提供程序概述和实现自定义MySQL ASP.NET标识存储提供程序的示例.您也应该检查出的代码RavenDB.AspNet.Identity NuGet包创建由大卫Boike在他提到的答案.源代码位于https://github.com/ILMServices/RavenDB.AspNet.Identity/tree/master/RavenDB.AspNet.Identity的 github上

4 个回答
  • 也许以下链接可以提供帮助:

    var claims = new List<Claim>();
    claims.Add(new Claim(ClaimTypes.Name, "Brock"));
    claims.Add(new Claim(ClaimTypes.Email, "brockallen@gmail.com"));
    var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);
    
    var ctx = Request.GetOwinContext();
    var authenticationManager = ctx.Authentication;
    authenticationManager.SignIn(id);
    

    2023-02-12 18:32 回答
  • 这就是我想出的.我很想知道这是否是完成这项任务的正确方法.

    在默认的MVC5网站上工作,我去了帐户控制器,找到了这个SignInAsync()功能.我把它调整如下:

        private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            //var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); --> this is where I want to get rid of UserManager
            List<Claim> claims = new List<Claim>{
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", user.Name), //user.Name from my database
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Id), //user.Id from my database
                new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "MyApplication"),
                new Claim("FirstName", user.FirstName) //user.FirstName from my database
            };
            ClaimsIdentity identity = new System.Security.Claims.ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
    
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }
    

    请记住,这还需要更改[HttpPost] Login函数以从用户数据库中获取用户而不是使用该UserManager.FindAsync()函数.

    这些调整后,默认站点的LogIn/LogOff部分似乎正常工作.在我接受之前,我会在这里留下这个答案,以防有人告诉我为什么我不应该这样做.

    2023-02-12 18:32 回答
  • 我认为你"正在做这件事".ASP.NET Identity Framework在设计时考虑了可插入的持久性.将RavenDB替换为EF的正确方法不是替换UserManager.相反,它是实现替代UserStore.因此AccountController,创建的线UserManager将改变:

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    

    至:

    public AccountController()
        : this(new UserManager<ApplicationUser>(new RavenDBUserStore<ApplicationUser>/* connection info here*/)))
    

    哪个是RavenDBUserStore您编写的实现类IUserStore,IUserPasswordStore以及您的特定应用程序所需的任何其他*Store接口.

    这种方法避免了您需要理解并重新实现所有内容,UserManager并确保您能够利用UserManagerMS 的未来改进.

    有关如何执行此操作的详细信息,请参阅ASP.NET标识的自定义存储提供程序概述和实现自定义MySQL ASP.NET标识存储提供程序的示例.您也应该检查出的代码RavenDB.AspNet.Identity NuGet包创建由大卫Boike在他提到的答案.源代码位于https://github.com/ILMServices/RavenDB.AspNet.Identity/tree/master/RavenDB.AspNet.Identity的 github上

    2023-02-12 18:33 回答
  • 我为新的ASP.NET MVC 5 Identity创建了一个RavenDB实现,并将其作为NuGet包发布.这可能对你有用.

    http://www.nuget.org/packages/RavenDB.AspNet.Identity/

    它有点预发布,但我在一个真实的项目中使用它.

    这是GitHub项目和RavenDB讨论组的一个简短线程.

    2023-02-12 18:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有