【发布时间】:2014-11-21 20:54:22
【问题描述】:
问题如下:通读以了解您需要帮助回答的内容...
我有
- VS 2013
- 带有 Razor 视图的 MVC 5
- Dapper DAL 自定义 UserManager 和 UserStore/User : IUser
我构建了我自己的 UserManager、RoleManager、UserStore、RoleStore,所有这些都基于 CustomUser : IUser 和 CustomRole : IRole
我可以更改密码、找回忘记的密码、登录、注销...一切正常。
但是,在剃刀视图中,当我尝试使用时:
User.IsInRole("Administrators");
我从其他模块中得到一个 SQL 异常(可能发生在 iPrincipal Identity 类中???):
System.Data.SqlClient.SqlException occurred
_HResult=-2146232060
_message=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)
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
_doNotReconnect=false
Class=20
LineNumber=0
Number=-1
Server=""
State=0
StackTrace:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
InnerException: <null>
但我可以打电话
var user = AsyncHelpers.RunSync<CloudUser>(() => UserManager.FindByNameAsync(User.Identity.Name));
ViewBag.IsInAdminRole = AsyncHelpers.RunSync<bool>(() => UserManager.IsInRoleAsync(user.Id, "Administrators"));
在我的控制器服务器代码中......
问题:
User.IsInRole 与 UserManager/UserStore 和/或 RoleManager 之间是什么关系
如何让 Razor 视图中的 User.IsInRole("Administrators") “了解”它需要使用我的自定义身份验证实现?
【问题讨论】:
-
当您将
@User.GetType().ToString()放入视图时,您会看到什么?即User下IPrincipal是什么类型? -
我知道你在想什么。我通过派生自己的 IPrincipal 和 WebView 页面自己解决了这个问题。我在这里使用了解决方案来解决我的问题:*.com/questions/18366566/…
-
啊,很好!正在输入一个答案,但你自己排序了!
-
添加你的答案 - 我会给你分数 - 我讨厌回答我自己的问题!!
标签: asp.net-identity iprincipal isinrole