42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNet.Authentication.Cookies;
|
|
using Microsoft.AspNet.Builder;
|
|
using Microsoft.AspNet.Http;
|
|
using Microsoft.Framework.DependencyInjection;
|
|
using Microsoft.Framework.Logging;
|
|
|
|
namespace CookieSample
|
|
{
|
|
public class Startup
|
|
{
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddAuthentication();
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
|
|
{
|
|
loggerfactory.AddConsole(LogLevel.Information);
|
|
|
|
app.UseCookieAuthentication(options =>
|
|
{
|
|
options.AutomaticAuthentication = true;
|
|
});
|
|
|
|
app.Run(async context =>
|
|
{
|
|
if (string.IsNullOrEmpty(context.User.Identity.Name))
|
|
{
|
|
var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }));
|
|
context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, user);
|
|
context.Response.ContentType = "text/plain";
|
|
await context.Response.WriteAsync("Hello First timer");
|
|
return;
|
|
}
|
|
|
|
context.Response.ContentType = "text/plain";
|
|
await context.Response.WriteAsync("Hello old timer");
|
|
});
|
|
}
|
|
}
|
|
} |