我有一个在ASP.NET MVC 4.5.2上运行的网站.我有一台IdentityServer4服务器正在运行但是当我尝试对它进行身份验证时,我得到一个:
invalid_request
Run Code Online (Sandbox Code Playgroud)
对于ASP.NET Core MVC,文档包含:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ClientId = "mvc",
SaveTokens = true
});
Run Code Online (Sandbox Code Playgroud)
我在我的项目Microsoft.Owin.Security.OpenIdConnect中包含以下NuGet包.我的代码如下:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",
SignInAsAuthenticationType = "Cookies",
Authority = "http://localhost:5000",
ClientId = "mvc",
});
Run Code Online (Sandbox Code Playgroud)
如何正确连接到它?
我试图按照此处的说明将Cookie身份验证添加到我的网站.
到目前为止,我添加了以下内容:
在Startup.cs文件的Configure方法中调用UseAuthentication方法:
app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)
在Startup.cs文件的ConfigureServices方法中调用AddAuthentication和AddCookie方法:
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie(options => {
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
Run Code Online (Sandbox Code Playgroud)
在我的登录代码中,我有
await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);
Run Code Online (Sandbox Code Playgroud)
principle是一个ClaimsPrincipal.
当我登录我的网站并拨打上面的行时,我收到错误消息:
InvalidOperationException:没有配置IAuthenticationSignInHandler来处理该方案的登录:MyCookieAuthenticationScheme
我错过了什么?
我试图在VS-2017中发布一个.NET项目.当我发布时,我得到错误:
构建中的所有构建提交必须使用源自同一项目集合的项目实例.
该项目在发布和调试模式下都可以正常运行.尝试发布为调试时会发生同样的事情.我有什么想法可以获得有关错误的更多信息吗?
我是Identity Server的新手,我的理解中缺少一个关键概念。我正在使用MVC教程中的代码。
如果我用属性装饰Home控制器[Authorize]并访问我的网站,则会重定向到IdentityServer。然后,使用我的用户名和密码登录。然后,我使用一些自定义代码并进行身份验证。我取回一个AccessToken,然后可以访问Home控制器。
我的客户端设置如下:
new Client {
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
ClientSecrets = new List<Secret>{new Secret("secret".Sha256())},
RequireConsent = false,
AccessTokenLifetime = 1,
// where to redirect to after login
RedirectUris = new List<string>{"http://localhost:5002/signin-oidc"},
// where to redirect to after logout
PostLogoutRedirectUris = new List<string>{"http://localhost:5002"},
AllowedScopes = new List<string>
{
StandardScopes.OpenId.Name,
StandardScopes.Profile.Name,
StandardScopes.OfflineAccess.Name,
}
}
Run Code Online (Sandbox Code Playgroud)
我的访问令牌是
{
"nbf": 1474697839,
"exp": 1474697840,
"iss": "http://localhost:5000",
"aud": "http://localhost:5000/resources",
"client_id": "mvc",
"scope": [ …Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法在c#7中做这样的事情
var test = "aaeag";
switch (test)
{
case test.StartsWith("a"):
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
可悲的是,它看起来不太可能.这是正确的还是我做错了什么?
在我的startup.cs方法中public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)我想访问HttpContext.
我想要这样做的原因是我想将401 响应重定向到登录页面。
app.UseStatusCodePages(async context => {
var request = context.HttpContext.Request;
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Unauthorized)
// you may also check requests path to do this only for specific methods
// && request.Path.Value.StartsWith("/specificPath")
{
response.Redirect("/account/login");
}
});
Run Code Online (Sandbox Code Playgroud)
context在上面的例子中没有属性HttpContext。我如何访问HttpContext?
我试图在基类上设置一个值,但我收到错误:
无法隐式转换
'PersonService'为'IMyInterface<IEntity>'.存在显式转换(您是否错过了演员?)
当我尝试创建PersonService()它时它确实实现了IMyInterface<Person>.人实施,IEntity所以我无法弄清楚为什么它不工作.
public interface IEntity { }
public class Person : IEntity { }
public interface IMyInterface<T> where T : IEntity { }
public class PersonService : IMyInterface<Person> { }
public abstract class Model : IEntity
{
public IMyInterface<IEntity> DataService { get; set; }
}
public class Test : Model
{
public Person Person { get; set; }
public Test() : base()
{
DataService = new PersonService();
}
}
Run Code Online (Sandbox Code Playgroud)