使用asp.net身份在业务层和添加属性 [英] Use asp.net identity in business layer and add properties

查看:196
本文介绍了使用asp.net身份在业务层和添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建金融应用程序。我使用asp.net的身份。当他们一个用户登录看到的将是他们公司的记录。我有GetRecords(INT用户ID),它返回的所有记录用户所在的公司。

 公开名单< FinanceData> GetRecords(INT用户id){
       INT companyID = userService.GetCompanyID(用户ID);
       financeService.GetFinanceData(companyID);
 }

我的问题是:

1-在asp.net的MVC层我能达到User.Identity.Name但业务层(IIb类)。我不能达到这个数据的属性。怎样才能达到这个?

2 - 我可以添加像User.Identity.CompanyID性质,我怎么能填补它,当用户登录。因为我不想去数据库中为每个请求。

我想用我的方法是这样的:

 公开名单< FinanceData> GetRecords(){
         financeService.GetFinanceData(User.Identity.CompanyID);
     }


解决方案

Asp.net身份使用要求来存储用户信息,这种模式是非常灵活的,你完全可以添加自定义声称它,就像CompanyId。你将不得不实施在 ApplicationUser 或者有史以来ClaimsIdentity被创建并添加自定义声明它。

(以下code从MVC模板复制)

 公共类ApplicationUser:IdentityUser
{
    公共异步任务< ClaimsIdentity> GenerateUserIdentityAsync(的UserManager< ApplicationUser>经理)
    {
        //注意authenticationType必须CookieAuthenticationOptions.AuthenticationType定义的匹配
        VAR的UserIdentity =等待manager.CreateIdentityAsync(这一点,DefaultAuthenticationTypes.ApplicationCookie);
        // **添加自定义的用户要求在这里**
        返回的UserIdentity;
    }
}

然后ClaimsPrincipal将通过 Thread.CurrentPrinciapl 随处可得在你的应用程序,它是在相同的AppDomain中运行。所以,如果你的其他层不作为服务运行的其他地方,你完全可以用

  VAR =本金作为Thread.CurrentPrincipal中ClaimsPrincipal

检查,如果这个不为空,如果它被验证,你要善于去。

要使它变得更加方便,您可以写的ClaimsPrincipal扩展方法为 .CompanyId 例如

I am creating finance app. I'm using asp.net identity. When a user logs in they'll just see their company's records. I have GetRecords(int userID) it returns all records for user's company.

public List<FinanceData> GetRecords(int userID){
       int companyID = userService.GetCompanyID(userID);
       financeService.GetFinanceData(companyID);
 }

My questions are:

1- In asp.net mvc layer I can reach User.Identity.Name but business layer (class lib.) I can't reach this data property. How can reach this?

2- Can I add properties like User.Identity.CompanyID and how can I fill it when user logs in. Because I don't want to go the database for each request.

I want to use my method like this:

public List<FinanceData> GetRecords(){
         financeService.GetFinanceData(User.Identity.CompanyID);
     }

解决方案

Asp.net identity uses Claims to store user information, this model is very flexible and you can totally add custom claims to it, like CompanyId. You would have to implement that in your ApplicationUser or where ever the ClaimsIdentity gets created and add your custom claims to it.

(following code is copied from the MVC template)

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // **Add custom user claims here**
        return userIdentity;
    }
}

Then ClaimsPrincipal will be available via Thread.CurrentPrinciapl everywhere in your app which is running in the same AppDomain. So if your other layer is not running as a service somewhere else, you can totally use

var principal = Thread.CurrentPrincipal as ClaimsPrincipal

check if this is not null and if it is authenticated, and you should be good to go.

To make it even more convenient you can write extension methods for the ClaimsPrincipal for .CompanyId for example.

这篇关于使用asp.net身份在业务层和添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆