登录MVC 5后,将用户添加到角色 [英] Add user to role after login MVC 5

查看:69
本文介绍了登录MVC 5后,将用户添加到角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是MVC新手,我试图在登录后将用户添加到角色中。



我设法在控制器中使用AuthorizeRoles操作,但是当尝试按照以下代码添加用户时,我收到一条错误消息,指出User Not找到。



Hi,

I am new to MVC and I am trying to add a user to a role after login.

I have managed to use the AuthorizeRoles on actions in a controller but when trying to add a user to a like as per the below code I get an error saying User Not Found.

System.Web.Security.Roles.AddUsersToRole(new string[] { httpContext.User.Identity.Name }, roles);





我的数据库中的用户表如下:



The Users Table in my database is as follows:

[Id]
      ,[FirstName]
      ,[LastName]
      ,[EmailAddress]
      ,[UserName]
      ,[Password]
      ,[DateCreated]
      ,[Gender]
      ,[ApplicationId]
      ,[IsAnonymous]
      ,[LastActivityDate]
      ,[UserId]





我试过通过任何用户的UserName,UserId和电子邮件地址,但它一直告诉我找不到任何已登录用户的用户。



在我的web.conf中ig我在System.Web中使用以下配置:

< rolemanager enabled =truedefaultprovider =SqlRoleProvider>

< providers>

< clear>

< add name =SqlRoleProvider>

connectionStringName =RolesCon

applicationName = /

type =System.Web.Providers.DefaultRoleProvider/>







还有其他人遇到过这种类型的问题吗?



我尝试了什么:



搜索谷歌,将rolemanager添加到web.config文件。



I have tried passing through the UserName, UserId and email address of any user but it keeps telling me user not found for any user that has been logged in.

In my web.config I am using the below configuration in System.Web:
<rolemanager enabled="true" defaultprovider="SqlRoleProvider">
<providers>
<clear>
<add name="SqlRoleProvider">
connectionStringName="RolesCon"
applicationName="/"
type="System.Web.Providers.DefaultRoleProvider"/>



Has anyone else experienced this type of an issue?

What I have tried:

Searched Google, added rolemanager to the web.config file.

推荐答案


我想象

Hi I imagine that
httpContext.User.Identity



未设置为登录用户。

您需要确保在您的应用程序中有一些代码正在设置它o正确的值。

一个例子是使用FormsAuthentication设置cookie并在web.config中配置FormsAuthentication以使其工作。


is not being set to the logged in user.
You need to make sure that in your application some code is setting it to correct value.
An example is to use FormsAuthentication to set the cookie and configure FormsAuthentication in web.config for it to work.


这篇关于登录MVC 5后,将用户添加到角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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