如何为 IdentityServer3 设置 MVC 客户端
How to setup an MVC client for IdentityServer3
我正在尝试使用 IdentityServer3,因此我将查看官方示例。我创建了一个非常简单的授权服务器:
namespace SimpleIdentityServer
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var options = new IdentityServerOptions
{
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get()),
RequireSsl = false
};
app.UseIdentityServer(options);
}
}
}
这是我记忆中的用户:
new Client
{
ClientName = "MVC application",
ClientId = "mvc",
Enabled = true,
AccessTokenType = AccessTokenType.Jwt,
Flow = Flows.Implicit,
ClientSecrets = new List<Secret>
{
new Secret("secret".Sha256())
},
AllowedScopes = new List<string>
{
"openId",
"profile"
},
RedirectUris = new List<string>
{
"http://localhost:12261/"
}
}
现在,我想使用上述服务器对 MVC 应用程序的用户进行身份验证,所以我这样做了:
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "http://localhost:47945/",
ClientId = "mvc",
RedirectUri = "http://localhost:12261/",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies"
});
}
这是一个用 Authorize
属性注释的示例控制器操作:
[Authorize]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
但是当我在我的 mvc 应用程序中转到 home/about 时,它向我显示 401 错误并且它似乎(从 serilog)它甚至没有调用授权服务器。
我认为可能发生的情况是您的 OWIN 管道未执行。您能否尝试在您的 Startup
class 中放置一个断点,终止 IIS 或 IIS Express - 无论您使用哪个 - 然后重新开始?
如果是这种情况,那么 IODC 中间件不会 捕获 HTTP 401
响应,因此不会将您重定向到您的 IdentityServer 实例。
一个可能的解释是当 运行 IIS 上的 ASP.NET 应用程序时,您没有包含启用 OWIN 的必要 NuGet 包。该包裹是 Microsoft.Owin.Host.SystemWeb
.
我正在尝试使用 IdentityServer3,因此我将查看官方示例。我创建了一个非常简单的授权服务器:
namespace SimpleIdentityServer
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var options = new IdentityServerOptions
{
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get()),
RequireSsl = false
};
app.UseIdentityServer(options);
}
}
}
这是我记忆中的用户:
new Client
{
ClientName = "MVC application",
ClientId = "mvc",
Enabled = true,
AccessTokenType = AccessTokenType.Jwt,
Flow = Flows.Implicit,
ClientSecrets = new List<Secret>
{
new Secret("secret".Sha256())
},
AllowedScopes = new List<string>
{
"openId",
"profile"
},
RedirectUris = new List<string>
{
"http://localhost:12261/"
}
}
现在,我想使用上述服务器对 MVC 应用程序的用户进行身份验证,所以我这样做了:
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "http://localhost:47945/",
ClientId = "mvc",
RedirectUri = "http://localhost:12261/",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies"
});
}
这是一个用 Authorize
属性注释的示例控制器操作:
[Authorize]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
但是当我在我的 mvc 应用程序中转到 home/about 时,它向我显示 401 错误并且它似乎(从 serilog)它甚至没有调用授权服务器。
我认为可能发生的情况是您的 OWIN 管道未执行。您能否尝试在您的 Startup
class 中放置一个断点,终止 IIS 或 IIS Express - 无论您使用哪个 - 然后重新开始?
如果是这种情况,那么 IODC 中间件不会 捕获 HTTP 401
响应,因此不会将您重定向到您的 IdentityServer 实例。
一个可能的解释是当 运行 IIS 上的 ASP.NET 应用程序时,您没有包含启用 OWIN 的必要 NuGet 包。该包裹是 Microsoft.Owin.Host.SystemWeb
.