分配角色后,Asp.NET身份用户未登录

时间:2018-07-16 18:43:44

标签: asp.net .net identity

我用asp.net创建了一个站点。身份登录在我测试过的计算机上工作正常。注册后,用户登录并发送到“留言板”页面,该页面检查用户是否具有角色,然后显示该消息或表明要联系以获得帐户批准的消息。分配角色后,返回到另一个页面并返回到留言板,仍显示原始消息。因此,我注销并成功完成登录并重定向,但登录到欢迎页面时,就服务器而言,用户尚未登录(我在页面上有一个标签,用于显示身份用户信息(它说什么也没找到))在iis中重新启动站点可以解决该问题。有关修复此问题的任何提示?

Public Class ApplicationSignInManager
    Inherits SignInManager(Of ApplicationUser, String)
    Public Sub New(userManager As ApplicationUserManager, authenticationManager As IAuthenticationManager)
        MyBase.New(userManager, authenticationManager)
    End Sub

    Public Overrides Function CreateUserIdentityAsync(user As ApplicationUser) As Task(Of ClaimsIdentity)
        Return user.GenerateUserIdentityAsync(DirectCast(UserManager, ApplicationUserManager))
    End Function

    Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationSignInManager), context As IOwinContext) As ApplicationSignInManager
        Return New ApplicationSignInManager(context.GetUserManager(Of ApplicationUserManager)(), context.Authentication)
    End Function
End Class

SignInManager

    Public Class SignInManager(Of TUser As {Class, IUser(Of TKey)}, TKey As 
    IEquatable(Of TKey))
    Implements IDisposable
    '
    ' Summary:
    '     Constructor
    '
    ' Parameters:
    '   userManager:
    '
    '   authenticationManager:
    Public Sub New(userManager As UserManager(Of TUser, TKey), authenticationManager As IAuthenticationManager)

    '
    ' Summary:
    '     AuthenticationType that will be used by sign in, defaults to DefaultAuthenticationTypes.ApplicationCookie
    Public Property AuthenticationType As String
    '
    ' Summary:
    '     Used to operate on users
    Public Property UserManager As UserManager(Of TUser, TKey)
    '
    ' Summary:
    '     Used to sign in identities
    Public Property AuthenticationManager As IAuthenticationManager

    '
    ' Summary:
    '     Dispose
    Public Sub Dispose()
    '
    ' Summary:
    '     If disposing, calls dispose on the Context. Always nulls out the Context
    '
    ' Parameters:
    '   disposing:
    Protected Overridable Sub Dispose(disposing As Boolean)

    '
    ' Summary:
    '     Called to generate the ClaimsIdentity for the user, override to add additional
    '     claims before SignIn
    '
    ' Parameters:
    '   user:
    Public Overridable Function CreateUserIdentityAsync(user As TUser) As Task(Of ClaimsIdentity)
    '
    ' Summary:
    '     Convert a TKey userId to a string, by default this just calls ToString()
    '
    ' Parameters:
    '   id:
    Public Overridable Function ConvertIdToString(id As TKey) As String
    '
    ' Summary:
    '     Convert a string id to the proper TKey using Convert.ChangeType
    '
    ' Parameters:
    '   id:
    Public Overridable Function ConvertIdFromString(id As String) As TKey
    '
    ' Summary:
    '     Creates a user identity and then signs the identity using the AuthenticationManager
    '
    ' Parameters:
    '   user:
    '
    '   isPersistent:
    '
    '   rememberBrowser:
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<SignInAsync>d__2))> <DebuggerStepThrough>
    Public Overridable Function SignInAsync(user As TUser, isPersistent As Boolean, rememberBrowser As Boolean) As Task
    '
    ' Summary:
    '     Send a two factor code to a user
    '
    ' Parameters:
    '   provider:
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<SendTwoFactorCodeAsync>d__6))> <DebuggerStepThrough>
    Public Overridable Function SendTwoFactorCodeAsync(provider As String) As Task(Of Boolean)
    '
    ' Summary:
    '     Get the user id that has been verified already or null.
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<GetVerifiedUserIdAsync>d__d))> <DebuggerStepThrough>
    Public Function GetVerifiedUserIdAsync() As Task(Of TKey)
    '
    ' Summary:
    '     Has the user been verified (ie either via password or external login)
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<HasBeenVerifiedAsync>d__11))> <DebuggerStepThrough>
    Public Function HasBeenVerifiedAsync() As Task(Of Boolean)
    '
    ' Summary:
    '     Two factor verification step
    '
    ' Parameters:
    '   provider:
    '
    '   code:
    '
    '   isPersistent:
    '
    '   rememberBrowser:
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<TwoFactorSignInAsync>d__14))> <DebuggerStepThrough>
    Public Overridable Function TwoFactorSignInAsync(provider As String, code As String, isPersistent As Boolean, rememberBrowser As Boolean) As Task(Of SignInStatus)
    '
    ' Summary:
    '     Sign the user in using an associated external login
    '
    ' Parameters:
    '   loginInfo:
    '
    '   isPersistent:
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<ExternalSignInAsync>d__1d))> <DebuggerStepThrough>
    Public Function ExternalSignInAsync(loginInfo As ExternalLoginInfo, isPersistent As Boolean) As Task(Of SignInStatus)
    '
    ' Summary:
    '     Sign in the user in using the user name and password
    '
    ' Parameters:
    '   userName:
    '
    '   password:
    '
    '   isPersistent:
    '
    '   shouldLockout:
    <AsyncStateMachineAttribute(GetType(SignInManager(Of Object, Object).<PasswordSignInAsync>d__29))> <DebuggerStepThrough>
    Public Overridable Function PasswordSignInAsync(userName As String, password As String, isPersistent As Boolean, shouldLockout As Boolean) As Task(Of SignInStatus)
End Class

Startup.Auth

 Public Sub ConfigureAuth(app As IAppBuilder)
        'Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(AddressOf ApplicationDbContext.Create)
        app.CreatePerOwinContext(Of ApplicationUserManager)(AddressOf ApplicationUserManager.Create)
        app.CreatePerOwinContext(Of ApplicationSignInManager)(AddressOf ApplicationSignInManager.Create)

        ' Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(New CookieAuthenticationOptions() With {
            .AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            .Provider = New CookieAuthenticationProvider() With {
                .OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(Of ApplicationUserManager, ApplicationUser)(
                    validateInterval:=TimeSpan.FromMinutes(30),
                    regenerateIdentity:=Function(manager, user) user.GenerateUserIdentityAsync(manager))},
            .LoginPath = New PathString("/Account/Login")})
        ' Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie)

        ' Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5))

        ' Enables the application to remember the second login verification factor such as phone or email.
        ' Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        ' This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie)

        ' Uncomment the following lines to enable logging in with third party login providers
        'app.UseMicrosoftAccountAuthentication(
        '    clientId:= "",
        '    clientSecret:= "")

        'app.UseTwitterAuthentication(
        '   consumerKey:= "",
        '   consumerSecret:= "")

        'app.UseFacebookAuthentication(
        '   appId:= "",
        '   appSecret:= "")

        'app.UseGoogleAuthentication(New GoogleOAuth2AuthenticationOptions() With {
        '   .ClientId = "",
        '   .ClientSecret = ""})
    End Sub

0 个答案:

没有答案