自定义授权& MVC 4中的角色无法正常工作 [英] Custom Authorization & Role in MVC 4 not working

查看:91
本文介绍了自定义授权& MVC 4中的角色无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义授权&在MVC 4中的角色不工作

1小时,52分钟前|链接





我需要实现单点登录应该调用Com +组件以对用户进行身份验证&提供角色。简而言之,我需要绕过MVC 4中的默认机制,它尝试访问aspnetdb数据库。所以我开始使用一个新的MVC4互联网项目,并添加了以下代码。



在Global.asax

  public   void  FormsAuthentication_OnAuthenticate( object  sender, FormsAuthenticationEventArgs args)
{
bool retval = CreateUserObject( John pwd);
}

private bool CreateUserObject( string userName, string password)
{
string [] currentUserRoles = { Admin 用户};
GenericPrincipal userPrincipal = new GenericPrincipal( new GenericIdentity(userName),currentUserRoles);
HttpContext.Current.User = userPrincipal;
// Thread.CurrentPrincipal = userPrincipal;
return true ;
}





在HomeController.cs中,我添加了关于操作的[授权]属性,如下所示它按预期工作



 [授权] 
public ActionResult关于()



但是如果我修改[Authorize]属性以仅允许Admin角色如下所示我得到运行时错误(在底部)。有没有办法解决这个问题,为登录用户使用我自己的角色集合,而不是查询数据库?我还需要做类似于用户配置文件的事情(即,而不是数据库,我应该填充Com +应用程序中的值。

 [授权(角色) =  管理员)] 
public ActionResult关于()










'/''应用程序中的
服务器错误。



建立与SQL Server的连接时发生网络相关或特定于实例的错误。服务器找不到或无法访问。验证实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26 - 找到指定的服务器/实例时出错)M

解决方案

首先要确保SQL Server服务正在运行。其他原因可能是,连接字符串不正确或某些Firewal l阻塞。



在这里,看一下这篇博文来解决这个问题: MSDN博客:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错 [ ^ ]



如果需要,请看这些:

例外 - 错误26 - 错误找到指定的服务器实例 [ ^ ]

提供程序:SQL网络接口,错误:26 - 查找服务器/实例时出错... [ ^ ]

未找到指定的SQL服务器o .. [ ^ ]

Custom Authorization & Role in MVC 4 not working
1 hour, 52 minutes ago|LINK

Hi,
I need to implement a Single Sign on where a Com+ component should be called to authenticate the user & provide the roles. In short, I need to bypass the default mechanism in MVC 4 where it tries to access the aspnetdb database. So I started with a new MVC4 internet project and added the following code.

In Global.asax

public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
    {
        bool retval = CreateUserObject("John", "pwd");
    }

private bool CreateUserObject(string userName, string password)
    {
        string[] currentUserRoles = { "Admin", "User" };
        GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(userName), currentUserRoles);
        HttpContext.Current.User = userPrincipal;
        //Thread.CurrentPrincipal = userPrincipal;
        return true;
     }



Within the HomeController.cs, I added the [Authorize] attribute for the "About" action as below and it works as expected

[Authorize]
public ActionResult About()


However if I modify the [Authorize] attribute to permit only "Admin" role as below I get a runtime error (at the bottom). Is there a way around this to use my own collection of roles for the logged in user, instead of querying the database? I also need to do something similar to the user Profile as well (i.e, instead of database, I should populate the values from the Com+ application.

[Authorize(Roles = "Admin")]
public ActionResult About()






Server Error in ''/'' Application.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)M

解决方案

First and foremost make sure that the SQL Server services are running. Other reasons could be, incorrect connection string or some Firewall blockage.

Here, have a look at this blog post to troubleshoot the issue : MSDN Blogs: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified[^]

If needed, look at these one:
Exception - error 26 - Error Locating Server Instance Specified[^]
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance...[^]
"Specified SQL server not found" o..[^]


这篇关于自定义授权& MVC 4中的角色无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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