DotVVM 身份验证
DotVVM Authentication
我在 dotVVM 框架中遇到了 Owin 身份验证问题。在需要认证的页面授权后出现401错误
这是我现在的startup.cs
var applicationPhysicalPath = HostingEnvironment.ApplicationPhysicalPath;
// use DotVVM
DotvvmConfiguration dotvvmConfiguration = app.UseDotVVM(applicationPhysicalPath);
dotvvmConfiguration.RouteTable.Add("Login", "", "Views/login.dothtml", null);
dotvvmConfiguration.RouteTable.Add("Home", "Home", "Views/home.dothtml", null);
dotvvmConfiguration.RouteTable.Add("Register", "Register", "Views/register.dothtml", null);
// use static files
app.UseStaticFiles(new StaticFileOptions()
{
FileSystem = new PhysicalFileSystem(applicationPhysicalPath)
});
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
});
HomeViewModel.cs
[Authorize]
public class HomeViewModel : DotvvmViewModelBase { }
我用这种方式创建了 Auth Cookie
public void Login()
{
var identity = LoginHelper.GetIdentity(Email, DataAccess.DbAccess.CreateHash(Password));
if (identity == null)
return;
Context.OwinContext.Authentication.SignIn(new ClaimsIdentity(identity));
Context.Redirect("Home");
}
OWIN 中的注册顺序很重要。
app.UseCookieAuthentication
应该是第一个注册的中间件。
我在 dotVVM 框架中遇到了 Owin 身份验证问题。在需要认证的页面授权后出现401错误
这是我现在的startup.cs
var applicationPhysicalPath = HostingEnvironment.ApplicationPhysicalPath;
// use DotVVM
DotvvmConfiguration dotvvmConfiguration = app.UseDotVVM(applicationPhysicalPath);
dotvvmConfiguration.RouteTable.Add("Login", "", "Views/login.dothtml", null);
dotvvmConfiguration.RouteTable.Add("Home", "Home", "Views/home.dothtml", null);
dotvvmConfiguration.RouteTable.Add("Register", "Register", "Views/register.dothtml", null);
// use static files
app.UseStaticFiles(new StaticFileOptions()
{
FileSystem = new PhysicalFileSystem(applicationPhysicalPath)
});
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
});
HomeViewModel.cs
[Authorize]
public class HomeViewModel : DotvvmViewModelBase { }
我用这种方式创建了 Auth Cookie
public void Login()
{
var identity = LoginHelper.GetIdentity(Email, DataAccess.DbAccess.CreateHash(Password));
if (identity == null)
return;
Context.OwinContext.Authentication.SignIn(new ClaimsIdentity(identity));
Context.Redirect("Home");
}
OWIN 中的注册顺序很重要。
app.UseCookieAuthentication
应该是第一个注册的中间件。