當前位置: 首頁>>代碼示例>>C#>>正文


C# AuthenticationIdentityManager類代碼示例

本文整理匯總了C#中AuthenticationIdentityManager的典型用法代碼示例。如果您正苦於以下問題:C# AuthenticationIdentityManager類的具體用法?C# AuthenticationIdentityManager怎麽用?C# AuthenticationIdentityManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AuthenticationIdentityManager類屬於命名空間,在下文中一共展示了AuthenticationIdentityManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CreateUser_Click

        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            var db = new ApplicationDbContext();
            var manager = new AuthenticationIdentityManager(new IdentityStore(db));
            ApplicationUser u = new ApplicationUser()
            { 
                UserName = userName,
                FirstName = tb_FirstName.Text,
                LastName = tb_LastName.Text,
                Email = tb_Email.Text
            };

            

            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success) 
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);

                var foundRole = db.Roles.Single(x => x.Name == dl_Roles.SelectedValue);
                u.Roles = new System.Collections.Generic.HashSet<UserRole>();
                u.Roles.Add(new UserRole()
                {
                    Role = foundRole,
                    User = u
                });
                db.SaveChanges();
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else 
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
開發者ID:Dyno1990,項目名稱:TelerikAcademy-1,代碼行數:35,代碼來源:Register.aspx.cs

示例2: LogIn

        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var context = new ApplicationDbContext();
                var signinUser = context.Users.FirstOrDefault(u => u.UserName == UserName.Text);

                if (signinUser!= null && !signinUser.IsDeleted)
                {
                    // Validate the user password
                    IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
                    IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);

                    if (result.Success)
                    {
                        OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    }
                    else
                    {
                        FailureText.Text = result.Errors.FirstOrDefault();
                        ErrorMessage.Visible = true;
                    }
                }
                else
                {
                    FailureText.Text = "Please register";
                    ErrorMessage.Visible = true;
                }
            }
        }
開發者ID:hristo11111,項目名稱:TelerikAcademy-HristoBratanov,代碼行數:30,代碼來源:Login.aspx.cs

示例3: LogIn

 protected void LogIn(object sender, EventArgs e)
 {
   
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
         IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
         if (result.Success)
         {
             var context = new ApplicationDbContext();
             var curUserName = UserName.Text;
             var userId = context.Users.FirstOrDefault(u => u.UserName == curUserName).Id;
             var userRole = context.UserRoles.FirstOrDefault(u => u.UserId == userId).Role.Name;
             if (userRole == "Banned")
             {
                 ErrorSuccessNotifier.AddErrorMessage("You are banned!");
                 return;
             }
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else
         {
             ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
         }
     }
 }
開發者ID:nnaidenov,項目名稱:TelerikAcademy,代碼行數:27,代碼來源:Login.aspx.cs

示例4: LogIn

 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
         IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else
         {
             var errorMessageText = result.Errors.FirstOrDefault();
             FailureText.Text = errorMessageText;
             ErrorMessage.Visible = true;
             ErrorSuccessNotifier.AddErrorMessage(errorMessageText);
         }
     }
     else
     {
         ErrorSuccessNotifier.AddErrorMessage(
             "An unexpected error occured! Please refresh the page...");
     }
 }
開發者ID:niki-funky,項目名稱:Telerik_Academy,代碼行數:25,代碼來源:Login.aspx.cs

示例5: CreateUser_Click

 protected void CreateUser_Click(object sender, EventArgs e)
 {
     string firstName = FirstName.Text;
     string lastName = LastName.Text;
     string userName = UserName.Text;
     string email = Email.Text;
     var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
     ApplicationUser u = new ApplicationUser()
     {
         FirstName = firstName,
         LastName = lastName,
         UserName = userName,
         Email = email
     };
     IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
     if (result.Success)
     {
         manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
         OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
     }
     else
     {
         ErrorMessage.Text = result.Errors.FirstOrDefault();
     }
 }
開發者ID:sabrie,項目名稱:TelerikAcademy,代碼行數:25,代碼來源:Register.aspx.cs

示例6: Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            //var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            //manager.Roles.CreateRoleAsync(new Role("Moderator"));
            //manager.Roles.AddUserToRoleAsync("54fbcab5-d65b-47d4-b517-2eba10002e21", "186c2138-de27-42a6-94f0-8b830c2e8b9c");
            var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            if (!manager.Logins.HasLocalLogin(User.Identity.GetUserId()))
            {
                grdMessages.EmptyDataTemplate = null;
                if (grdMessages.FooterRow != null)
                {
                    grdMessages.FooterRow.Visible = false;
                }
            }
            else
            {
                var userId = User.Identity.GetUserId();

                var db = new ApplicationDbContext();
                var user = db.Users.FirstOrDefault(u => u.Id == userId);
                if (user != null)
                {
                    if (user.Roles.Any(r => r.Role.Name == "Moderator"))
                    {
                        grdMessages.Columns[2].Visible = true;
                    }
                    else if (user.Roles.Any(r => r.Role.Name == "Administrator"))
                    {
                        grdMessages.Columns[2].Visible = true;
                        grdMessages.Columns[3].Visible = true;
                    }
                }
            }
        }
開發者ID:NikolayKostadinov,項目名稱:TelerikAkademy,代碼行數:34,代碼來源:Default.aspx.cs

示例7: CreateUser_Click

        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            var manager = new AuthenticationIdentityManager(new IdentityStore());
            User u = new User(userName) { UserName = userName };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                //OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    UserDetail details = new UserDetail();
                    context.UserDetails.Add(details);
                    context.SaveChanges();
                    var user = context.AspNetUsers.Find(u.Id);
                    user.UserDetailsId = details.UserDetailsId;
                    context.SaveChanges();
                }

                Response.Redirect("~/Account/UserDetails");
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
開發者ID:nikimoto,項目名稱:JadeiteSocialNetwork,代碼行數:27,代碼來源:Register.aspx.cs

示例8: LinkButtonBanUser_Command

        protected void LinkButtonBanUser_Command(object sender, CommandEventArgs e)
        {
            var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));

            string roleBanId = "2";
            manager.Roles.AddUserToRoleAsync(e.CommandArgument.ToString(), roleBanId);
            
        }
開發者ID:nnaidenov,項目名稱:TelerikAcademy,代碼行數:8,代碼來源:Users.aspx.cs

示例9: CreateUser_Click

        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;

            var manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext()));
            ApplicationUser u = new ApplicationUser(userName)
            {
                UserName = userName,
                FirstName = this.TextBoxFirstName.Text,
                LastName = this.TextBoxLastName.Text,
                Email = this.TextBoxEmail.Text,
                JoinDate = DateTime.Now,

            };
            var context = new AcademyDbContext();

            string fileName = string.Empty;

            var fileUpload = this.FileUploadAvatar;
            if (fileUpload.HasFile)
            {
                if (fileUpload.PostedFile.ContentLength < 102400 &&
                (fileUpload.PostedFile.ContentType == PngImageFormat ||
                fileUpload.PostedFile.ContentType == JpegImageFormat ||
                fileUpload.PostedFile.ContentType == GifImageFormat))
                {
                    fileName = userName.Replace("<", string.Empty).Replace(">", string.Empty) + GetAvatarExtension(FileUploadAvatar.PostedFile.FileName);
                    fileUpload.SaveAs(Server.MapPath(MainPath) + fileName);
                    u.AvatarPath = MainPath + fileName;
                }
                else
                {
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddErrorMessage("The uploaded avatar exceeds 100KB or is in a wrong format.");
                    Response.Redirect(Request.RawUrl, false);
                    return;
                }
            }
            else
            {
                u.AvatarPath = DefaultImagePath;
            }

            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                ErrorSuccessNotifier.ShowAfterRedirect = true;
                ErrorSuccessNotifier.AddSuccessMessage("Registration completed successfully.");
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
            }
        }
開發者ID:BobbyBorisov,項目名稱:ChrysoberylAcademySystem,代碼行數:56,代碼來源:Register.aspx.cs

示例10: AccountController

 public AccountController() 
 {
     var context = new MoviesContext();
     userRole = context.Roles
                       .Where(r => r.Name == "user")
                       .Select(r => r.Id)
                       .FirstOrDefault()
                       .ToString();
     IdentityManager = new AuthenticationIdentityManager(new IdentityStore(context));
 }
開發者ID:ben12345,項目名稱:Movie-Room,代碼行數:10,代碼來源:AccountController.cs

示例11: Page_Load

        protected void Page_Load()
        {
            // Process the result from an auth provider in the request
            ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request);
            if (String.IsNullOrEmpty(ProviderName))
            {
                Response.Redirect("~/Account/Login");
            }
            if (!IsPostBack)
            {
                IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
                var auth = Context.GetOwinContext().Authentication;
                ClaimsIdentity id = manager.GetExternalIdentity(auth);
                var context = new ApplicationDbContext();
                var user = context.UserRoles.FirstOrDefault(u => u.User.UserName == id.Name);
                if (user != null)
                {
                    var userRole = user.Role.Name;
                    if (userRole == "Banned")
                    {
                        ErrorSuccessNotifier.AddErrorMessage("You are banned!");
                        ErrorSuccessNotifier.ShowAfterRedirect = true;
                        Response.Redirect("~/Account/Login");
                    }
                }
                IdentityResult result = manager.SignInExternalIdentity(auth, id);

                if (result.Success)
                {
                    OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else if (User.Identity.IsAuthenticated)
                {
                    result = manager.LinkExternalIdentity(id, User.Identity.GetUserId());
                    if (result.Success)
                    {
                        OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    }
                    else
                    {
                        AddErrors(result);
                        return;
                    }
                }
                else
                {
                    userName.Text = id.Name;
                }
            }
        }
開發者ID:nnaidenov,項目名稱:GoldstoneForum,代碼行數:50,代碼來源:RegisterExternalLogin.aspx.cs

示例12: Redirect

 private async void Redirect()
 {
     AuthenticationIdentityManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
     var userId = User.Identity.GetUserId();
     var roles = await manager.Roles.GetRolesForUserAsync(userId);
     if (roles.Any(r => r.Name == "Administrator"))
     {
         Response.Redirect("~/Administrator/AdministratorDefault.aspx");
     }
     else if (roles.Any(r => r.Name == "Moderator"))
     {
         Response.Redirect("~/Moderator/ModeratorDefault.aspx");
     }
 }
開發者ID:Gerya,項目名稱:TelerikAcademy,代碼行數:14,代碼來源:Default.aspx.cs

示例13: CreateUser_Click

        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            string email = Email.Text;
            string filename = "default.png";
            if (UploadAvatar.HasFile)
            {
                if (UploadAvatar.PostedFile.ContentType == "image/jpeg" ||
                    UploadAvatar.PostedFile.ContentType == "image/gif" ||
                    UploadAvatar.PostedFile.ContentType == "image/png")
                {
                    if (UploadAvatar.PostedFile.ContentLength < 102400)
                    {
                        filename = userName + Path.GetExtension(UploadAvatar.FileName);
                        UploadAvatar.SaveAs(Server.MapPath("~/Avatar_Files/") + filename);
                    }
                    else
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Upload status: The file has to be less than 100 kb!");
                        return;
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Upload status: Only JPEG files are accepted!");
                    return;
                }
            }

            var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            ApplicationUser u = new ApplicationUser()
            {
                UserName = userName,
                Email = email,
                Avatar = filename

            };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
            }
        }
開發者ID:nnaidenov,項目名稱:GoldstoneForum,代碼行數:48,代碼來源:Register.aspx.cs

示例14: CreateUser_Click

 protected void CreateUser_Click(object sender, EventArgs e)
 {
     string userName = UserName.Text;
     var manager = new AuthenticationIdentityManager(new IdentityStore());
     User u = new User(userName) { UserName = userName };
     IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
     if (result.Success)
     {
         manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
         OpenAuthProviders.RedirectToReturnUrl("~/library-system/Default.aspx", Response);
     }
     else
     {
         ErrorMessage.Text = result.Errors.FirstOrDefault();
     }
 }
開發者ID:krasiuzunov7,項目名稱:WebFormsBookLibrary,代碼行數:16,代碼來源:Register.aspx.cs

示例15: CreateUser_Click

 protected void CreateUser_Click(object sender, EventArgs e)
 {
     string userName = this.UserName.Text;
     var manager = new AuthenticationIdentityManager(new IdentityStore(new BooksLibrarySystemContext()));
     User u = new User(userName) { UserName = userName };
     IdentityResult result = manager.Users.CreateLocalUser(u, this.Password.Text);
     if (result.Success)
     {
         manager.Authentication.SignIn(this.Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
         this.Response.Redirect(this.Request.QueryString["ReturnUrl"], false);
         //BooksLibrarySystem.Web.Account.OpenAuthProviders.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
     }
     else
     {
         this.ErrorMessage.Text = result.Errors.FirstOrDefault();
     }
 }
開發者ID:stoskov,項目名稱:books-library-system,代碼行數:17,代碼來源:Register.aspx.cs


注:本文中的AuthenticationIdentityManager類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。