简体   繁体   中英

How to make/create owin authentication middleware in asp.net vnext(MVC 6)?

I was implementing owin authentication in login page but I want middle ware of owin authentication in asp.net vnext (MVC6) because I want to redirect into default(Login) page when user login state as stay long time.The below code is the reference.

var claims = new List<Claim>
                    {
                        new Claim("UserId",user.Id),
                        new Claim("UserName",user.UserName),
                        new Claim("Roles",user.UserRoles)
                    };
                    var ident = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie,user.UserName,user.UserRoles);
                    var authProp = new AuthenticationProperties() {IsPersistent=true,AllowRefresh=true,ExpiresUtc=System.DateTime.Now.AddMinutes(1).ToLocalTime(),RedirectUri="/Account/Login"};
                    IOwinContext owinContext = new OwinContext();
                    owinContext.Authentication.SignIn(authProp, ident);

Thanks in Advance.

If you are looking for Default Login Page if user is not logged In , than you can use [Authorize] attribute above the controller.

namespace Demo.Controllers
{   
 [Authorize]
 public class DemoController : Controller
 {
  // Your Code
 }
}

This Should help you..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM