简体   繁体   English

Asp.net Web 应用程序基于角色的身份验证

[英]Asp.net Web Application Role based authentication

I am kinda new at Asp.net and I have been trying to create a simple Transaction based system wherein users with different roles will have different menus and categories available for them.我是 Asp.net 的新手,我一直在尝试创建一个简单的基于事务的系统,其中具有不同角色的用户将有不同的菜单和类别可供他们使用。 Currently, I am using this tutorial for my authenticated based login.目前,我正在使用教程进行基于身份验证的登录。

The code is working but I have noticed that the database being used is the default connection.该代码正在运行,但我注意到正在使用的数据库是默认连接。 What I wanted to do was create my own database and use it as the default connection (MyOwnConnectionString).我想做的是创建自己的数据库并将其用作默认连接(MyOwnConnectionString)。 How can I do that?我怎样才能做到这一点?

I am using VS 2015 and Sql Server 2016. I have a two tables namely Tbl_User and Tbl_Roles where the two tables are linked with the role_id我正在使用 VS 2015 和 Sql Server 2016。我有两个表,即Tbl_UserTbl_Roles ,这两个表与role_id链接

UPDATE更新

The tutorial above is about using the default registration of Asp.net wherein if the username to be used for registration matches the declared variable (in this case Admin ), the user is given the role of Administrator in the Default Connection under tables AspNetUser, AspNetUserRoles, AspNetRoles.上面的教程是关于使用 Asp.net 的默认注册,其中如果要用于注册的用户名与声明的变量匹配(在本例中为Admin ),则用户在表 AspNetUser、AspNetUserRoles、AspNetRoles 下的默认连接中被赋予管理员角色.

I have a table named Tbl_User with columns "Name, Username, Password, Role"我有一个名为Tbl_User的表,其中包含"Name, Username, Password, Role"

Whenever I try to login, the database to be used is my created database(DB_Transaction) and not the default connect of Asp(DefaultConnection DB) and search for the corresponding roles in Tbl_Roles whether Admin or User then redirect them to their corresponding pages.每当我尝试登录时,要使用的数据库是我创建的数据库(DB_Transaction)而不是 Asp(DefaultConnection DB)的默认连接,并在Tbl_Roles中搜索相应的角色,无论是管理员还是用户,然后将它们重定向到相应的页面。

What should happen应该发生什么

If you want to use Asp.net Identity authentication and authorization then we need not to create table own self,the identity does it for us after user is registered.如果你想使用 Asp.net 身份认证和授权,那么我们不需要自己创建表,用户注册后身份为我们做。 And login is handelled along with the authorization.Just specify the database name in ConnectionString and for now you can use DefaultConnection as ConnectionString Key.Add the register a dummy user.You should see tables in your specified database.并且登录与授权一起处理。只需在 ConnectionString 中指定数据库名称,现在您可以使用 DefaultConnection 作为 ConnectionString 键。添加注册一个虚拟用户。您应该看到指定数据库中的表。

Create database in SQL 2016 Add that connection string in WEB config file .在 SQL 2016 中创建数据库 在WEB 配置文件中添加该连接字符串 After pass that connection string to your SQL Connection Object or Database Context for Entity framework.将该连接字符串传递给您的 SQL 连接 Object 或实体框架的数据库上下文后。 here is reference link 这是参考链接

  <connectionStrings> 
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WingtipToys;Integrated Security=True" providerName="System.Data.SqlClient" /> 
<add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\wingtiptoys.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> 

you can change the default connection from web.config file open it an go to connection string and remove the default connection and copy your server name connection at SQL and paste in the connection string and u can change the database name as u can see in this image start remove the default connection from the local word enter image description here您可以从 web.config 文件更改默认连接打开它一个 go 到连接字符串并删除默认连接并将您的服务器名称连接复制到 SQL 并粘贴到数据库中image start 从本地单词中删除默认连接 在此处输入图像描述

enter image description here在此处输入图像描述

this is the server name connection这是服务器名称连接

You can use the web.config file to update your connection string or use it in your code as well您可以使用 web.config 文件来更新您的连接字符串或在您的代码中使用它

using System.Data.SqlClient;

SqlConnection connection = new SqlConnection();
connection.ConnectionString ="Data Source=ServerName;" +"InitialCatalog=DataBaseName;" +
"Integrated Security=SSPI;";
connection.Open();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net基于mvc角色的身份验证 - Asp.net mvc role based authentication 基于ASP.NET Web API角色的身份验证自定义属性,检查用户是否在角色中 - asp.net web api role based authentication custom attribute check if user in role 没有web.sitemap,是否可以具有基于ASP.NET角色的表单身份验证和授权? - Is it Possible to have ASP.NET role based form authentication and authorization without web.sitemap? 基于asp.net中用户角色的Web应用程序中的表单身份验证工具 - Tools for form authentication in web application on the basis of user role in asp.net 如何从启用基于场的身份验证的ASP.NET应用程序或ASP.NET MVC对基于SharePoint声明的Web应用程序进行身份验证 - How to authenticate sharepoint claims based web application from farm based authentication enabled asp.net application or asp.net mvc asp.net中具有窗口身份验证的基于角色的授权 - role based authorization with window authentication in asp.net 基于ASP.NET Web API角色的安全性 - ASP.NET Web API role based security 具有基于角色的授权的ASP.NET Web Api - ASP.NET Web Api with Role based authorization 如何在Web API应用程序中使用ASP.net 5 Identity? 基于令牌的用户身份验证。 移动应用 - How to use ASP.net 5 Identity in web API application? User authentication based on tokens. Mobile apps 如何从 ASP.NET Web 应用程序注销,如果基于 webapi 令牌的身份验证达到其过期时间 - How to logout from ASP.NET web application, if webapi token based authentication reaches it's expire time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM