Windows身份验证,并通过数据库中添加授权角色 - MVC asp.net [英] Windows Authentication and add Authorization Roles through database - MVC asp.net

查看:77
本文介绍了Windows身份验证,并通过数据库中添加授权角色 - MVC asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来mvc4的ASP.NET和困惑的身份验证和授权。我们的是一个内部网站,从Windows身份验证需要用户名(HttpContext.Current.User.Identity.Name),并核对数据库,如果用户名存在,并且用户有什么样的角色。我想使用全局[授权]属性和角色,给访问控制器。谁能帮我对如何下手。

I am new to mvc4 asp .net and confused with the authentication and authorization. Our's is an internal website that takes username(HttpContext.Current.User.Identity.Name) from windows authentication and check against database if the username exists and what roles the user has. I want to use the global [Authorize] attribute and roles, to give access to controllers. Can anyone help me on how to start with.

现在,我有通行证用户名,并从数据库中的用户数据和相关的角色,查询数据添加到model.So,我使用这个功能给访问该网站的功能,但我想使用相同的数据来检查对所有控制器和视图查询没有对数据库的所有时间。

For now, I have a function that passes username and get the user data and related roles from database, the query data is added to the model.So, I am using this function to give access to the website but I want to use the same data to check against all controllers and views without querying against db all the time.

推荐答案

您只需要创建自定义角色提供商。您可以通过创建从 System.Web.Security.RoleProvider 和压倒一切的某些成员继承的类做到这一点。下面code应该让你开始。替换所有抛出新NotImplementedException()东西你实现的功能。例如,的isUserInRole 您将提供code,将查询数据库,看看用户是否在指定的作用。

You just need to create a custom role provider. You do this by creating a class that inherits from System.Web.Security.RoleProvider and overriding certain members. The below code should get you started. Replace all the throw new NotImplementedException() stuff with your implementation of the function. For example, IsUserInRole you would provide code that would query your database to see if the user is in the specified role.

using System.Web.Security;

namespace MyNamespace
{        
    public class MyRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
           get; set;
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

您可能不需要实现这一切。例如,如果你的申请将不会被创建或删除角色,则没有必要做 CREATEROLE什么 DeleteRole

You may not need to implement all of it. For example, if your application won't be creating or deleting Roles, then no need to do anything with CreateRole or DeleteRole.

您也需要,所以它知道要利用它来注册ASP.NET Framework的角色提​​供。更新的web.config 象下面这样。

You also need to register your role provider with ASP.NET framework so it knows to make use of it. Update your web.config like below.

<configuration>
    <system.web>
        <roleManager defaultProvider="MyRoleProvider">
            <providers>
                <add
                    name="MyRoleProvider"
                    type="MyNamespace.MyRoleProvider"           
                    applicationName="MyApplicationName" />
            </providers>
        </roleManager>
    </system.web>
</configuration>

这篇关于Windows身份验证,并通过数据库中添加授权角色 - MVC asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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