Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [assembly: OwinStartup(typeof(IoTWeb.App_Start.Startup))]
- namespace IoTWeb.App_Start
- {
- public class Startup
- {
- private const int DEFAULTTIMEOUT = 5;
- private const int DEFAULTEXPIRETIMESPAN = 5;
- public void Configuration(IAppBuilder app)
- {
- // Enable the application to use a cookie to store information for the signed in user
- app.UseCookieAuthentication(new CookieAuthenticationOptions
- {
- AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
- LoginPath = new PathString("/Account/Login")
- });
- app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.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
- {
- OnValidateIdentity =
- SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(TimeSpan.FromMinutes(DEFAULTTIMEOUT),
- (manager, user) => Task.FromResult(manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie)))
- },
- SlidingExpiration = true,
- ExpireTimeSpan = TimeSpan.FromMinutes(DEFAULTEXPIRETIMESPAN)
- });
- // Use a cookie to temporarily store information about a user logging in with a third party login provider
- app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
- }
- }
- public class UserStoreService : IUserStore<User>, IUserPasswordStore<User>, IUserEmailStore<User>
- {
- private readonly TenantEntities context = new TenantEntities();
- public Task<User> Find(string userName, string password)
- {
- Task<User> task = context.User.Where(
- apu => apu.UserName == userName && apu.Password == password)
- .FirstOrDefaultAsync();
- return task;
- }
- public ActionResult Login(LoginViewModel model, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
- User user = manager.Find(model.UserName, model.Password);
- if (user != null)
- {
- IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
- authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
- ClaimsIdentity identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
- authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
- return RedirectToLocal(returnUrl);
- }
- }
- // If we got this far, something failed, redisplay form
- ModelState.AddModelError("", "Login failed due to incorrect credentials.");
- return View(model);
- }
- [AllowAnonymous]
- public ActionResult Register()
- {
- // Remove the Cookie in Here as he goes a bit further
- if (ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("FinancesModelDataCookie"))
- {
- HttpCookie cookie = ControllerContext.HttpContext.Request.Cookies["FinancesModelDataCookie"];
- if (cookie != null)
- {
- cookie.Expires = DateTime.Now.AddDays(-1);
- ControllerContext.HttpContext.Response.Cookies.Add(cookie);
- }
- }
- return View();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement