C# 使用[Authorize(Roles=";Edit";时获取sql错误]
我最近在一个MVC网站上实现了asp.net identity 2。我按照本演练将pk更改为INT 一切似乎都很好,我可以按如下方式为用户分配角色:C# 使用[Authorize(Roles=";Edit";时获取sql错误],c#,asp.net,asp.net-mvc,asp.net-identity-2,C#,Asp.net,Asp.net Mvc,Asp.net Identity 2,我最近在一个MVC网站上实现了asp.net identity 2。我按照本演练将pk更改为INT 一切似乎都很好,我可以按如下方式为用户分配角色: var result = await userManager.AddToRoleAsync(User.Identity.GetUserId<int>(), "Edit"); 这是我的启动 public void ConfigureAuth(IAppBuilder app) { // Con
var result = await userManager.AddToRoleAsync(User.Identity.GetUserId<int>(), "Edit");
这是我的启动
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(IdentityDbContext.Create);
app.CreatePerOwinContext<FskUserManager>(FskUserManager.Create);
app.CreatePerOwinContext<FskSignInManager>(FskSignInManager.Create);
app.CreatePerOwinContext<FskRoleManager>(FskRoleManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<FskUserManager, FskUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) =>
user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (id.GetUserId<int>()))
//OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<FskUserManager, FskUser,int>(
// validateInterval: TimeSpan.FromMinutes(30),
// regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
public void ConfigureAuth(IAppBuilder应用程序)
{
//将数据库上下文、用户管理器和登录管理器配置为每个请求使用一个实例
app.CreatePerOwinContext(IdentityDbContext.Create);
app.CreatePerOwinContext(FskUserManager.Create);
app.CreatePerOwinContext(fsksignnmanager.Create);
app.CreatePerOwinContext(FskRoleManager.Create);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentityCallback:(管理者,用户)=>
user.GenerateUserIdentityAsync(管理器),
getUserIdCallback:(id)=>(id.GetUserId())
//OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
//validateInterval:TimeSpan.FromMinutes(30),
//regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
是什么导致了我的问题?我觉得我遗漏了一个重要的步骤,我告诉asp.net框架使用我的RoleStore而不是默认的RoleStore
这是我的连接字符串:
<add name="IdentityDbContext" connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=FSK_FskNetworks;persist security info=True;user id=sa;password=p@ssword;MultipleActiveResultSets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
在我的连接字符串上方添加
后
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
Source Error:
Line 236: <membership>
Line 237: <providers>
Line 238: <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
Line 239: </providers>
Line 240: </membership>
Source File: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config Line: 238
配置错误
描述:处理服务此请求所需的配置文件时出错。请查看下面的特定错误详细信息,并适当修改配置文件。
分析器错误消息:在应用程序配置中找不到连接名称“LocalSqlServer”,或者连接字符串为空。
源错误:
第236行:
第237行:
第238行:
第239行:
第240行:
源文件:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.Config行:238
您的machine.config
文件已包含某些值,例如连接字符串、成员资格提供程序和角色管理器设置,并且您的web.config
文件继承了其中的所有设置
计算机上的machine.config
文件设置为使用名为LocalSqlServer
的连接字符串添加成员资格提供程序。因此,当您使用Authorize
属性时,它首先尝试使用该提供者,而不是Identity
。角色经理也是如此
您可以通过如下方式清除web.config
中的现有提供程序来禁用此功能:
....
...
此外,您可能需要将其添加到appSettings
:
您的
machine.config
文件已包含某些值,例如连接字符串、成员资格提供程序和角色管理器设置,并且您的web.config
文件继承了其中的所有设置
计算机上的machine.config
文件设置为使用名为LocalSqlServer
的连接字符串添加成员资格提供程序。因此,当您使用Authorize
属性时,它首先尝试使用该提供者,而不是Identity
。角色经理也是如此
您可以通过如下方式清除web.config
中的现有提供程序来禁用此功能:
....
...
此外,您可能需要将其添加到appSettings
:
如果在连接字符串上方添加
行,是否有效?添加
后,我没有收到其他错误。我将添加到我的问题中。它不在我的web.config中。。。如果您看到错误消息,它引用machine.config是出于某种奇怪的原因吗?哦,是的,然后将另一个
添加到web.config
以删除成员资格提供程序。清除与我的错误有什么关系?所以我可以试着理解你认为的问题是什么?连接字符串与标识系统的其余部分一起工作?如果在连接字符串上方添加
行,它是否工作?添加
后,我没有收到其他错误。我将添加到我的问题中。它不在我的web.config中。。。如果您看到错误消息,它引用machine.config是出于某种奇怪的原因吗?哦,是的,然后将另一个
添加到web.config
以删除成员资格提供程序。清除与我的错误有什么关系?所以我可以试着理解你认为的问题是什么?连接字符串与标识系统的其余部分一起工作?我在哪个部分下
<add name="IdentityDbContext" connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=FSK_FskNetworks;persist security info=True;user id=sa;password=p@ssword;MultipleActiveResultSets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
Source Error:
Line 236: <membership>
Line 237: <providers>
Line 238: <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
Line 239: </providers>
Line 240: </membership>
Source File: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config Line: 238