轻松将 ASP.NET Identity 用于角色提供程序 [英] Using ASP.NET Identity for a Role Provider easily
问题描述
最近两天我刚刚用我现有的数据库研究和实现了新的 ASP.NET Identity 系统.更多相关信息:将 ASP.NET 身份集成到现有 DbContext.
I just spent the last two days researching and implementing the new ASP.NET Identity system with my existing database. More on that here: Integrating ASP.NET Identity into Existing DbContext.
现在,我有一个可用的 UserStore
和 RoleStore
,但我似乎无法弄清楚如何在不编写的情况下在我的 ASP.NET MVC 5 应用程序中利用它们在所有让我感到困惑的身份示例中,似乎有大量代码.
Now, I have a working UserStore
and RoleStore
, but I can't seem to figure out how to leverage them in my ASP.NET MVC 5 application without writing what seems like colossal amounts of code like in all the Identity samples that confuse me.
我想要实现两件事:1) 使用 cookie 来维护授权,2) 使用角色来限制应用程序对视图和控制器中呈现的内容的访问.
There's two things I want to achieve: 1) use cookies to maintain the authorizations and 2) use roles to limit application access both in what is rendered in the views and on the controllers.
为了能够使用那些,我显然需要使用 Controller.User
属性,它代表授权用户并查看它的角色.如何让我的身份实现实现这一目标?
To be able to use those I need to obviously use the Controller.User
property which represents the authorized user and peek into it's roles. How do I get my Identity implementation to make that happen?
最后,在 Identity 示例中,我看到他们正在使用 OWIN,我有点理解,但这似乎是一种超级迂回的方式,我仍然不知道如何正确实施.就索赔而言,他们让我困惑的程度是我理解他们的两倍.
Lastly, in the Identity samples I see they're using OWIN, which I kind of get, but it seems like it's a super roundabout way, which I still don't get how to properly implement. As far as Claims, they confuse me twice as much as I understand them.
如果您有任何正确方向的指示,我将不胜感激.
I'd appreciate any pointers in the right direction.
推荐答案
回到这个问题之后,我想我想出了一个简单有效的解决方案.我最终为 OWIN 创建了一个启动配置类.据我了解,由于 OWIN 是一个中间件,它会拦截请求,找出身份验证(如果有),并更新 Controller
类的 User
属性,其中 User
是 ClaimsIdentity
类的实例.
After going back into this I think I figured out a solution that simply works. I ended up creating a startup configuration class for OWIN. From what I understand since OWIN is a middleware it intercepts the requests, figures out the authentication (if any), and updates the User
property of the Controller
classes where User
is an instance of a ClaimsIdentity
class.
之后,其他一切都像您通常使用 ASP.NET 的 User
属性一样工作.我确实使用名为 UserId
的额外属性扩展了我的基本控制器,该属性解析 User
属性以获取数据库中使用的实际 Id.我的目的是让我可以使用 Id 来查询我的 DbContext
使用的真正的 Employee
类.我们将看看它是否保持不变,同时,这是我的 StartupConfiguration
的代码:
After that everything else works just as you would normally use the User
property of ASP.NET. I did extend my base Controller with an extra property called UserId
which parses the User
property to get the actual Id being used in the database. My intention with that is to have the Id available to me to query for the real Employee
class that my DbContext
uses. We'll see if that stays or not, in the mean time, here's the code for my StartupConfiguration
:
public sealed class StartupConfig {
public void Configuration(
IAppBuilder app) {
this.ConfigureAuthentication(app);
}
public void ConfigureAuthentication(
IAppBuilder app) {
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/"),
ExpireTimeSpan = new TimeSpan(0, 60, 0)
});
}
}
这是我如何配置我的 UserId
属性:
Here's how I configured my UserId
property:
protected int UserId {
get {
return Convert.ToInt32(base.User.Identity.GetUserId());
}
}
不要忘记用 [assembly: OwinStartupAttribute(typeof(namespace.StartupConfig))]
装饰类.希望这对某人有所帮助.
Don't forget to decorate the class with [assembly: OwinStartupAttribute(typeof(namespace.StartupConfig))]
. Hope this helps somebody.
这篇关于轻松将 ASP.NET Identity 用于角色提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!